admin管理员组

文章数量:1355037

I am trying to reload the page by hitting the same URL of the app but getting this error in googleappscript, previously it was working fine but not it has stopped working

function reLoad() {
       google.script.run
       .withSuccessHandler(function(){
        window.open("/macros/s/AKfycbxqBb8dv488gLQjYCmJDGJ98EiX3Ipo5pUHmAHoRmY/dev","_top");
      })
}

I am trying to reload the page by hitting the same URL of the app but getting this error in googleappscript, previously it was working fine but not it has stopped working

function reLoad() {
       google.script.run
       .withSuccessHandler(function(){
        window.open("https://script.google./a/google./macros/s/AKfycbxqBb8dv488gLQjYCmJDGJ98EiX3Ipo5pUHmAHoRmY/dev","_top");
      })
}
Share Improve this question edited Sep 22, 2021 at 22:12 TheMaster 51.1k7 gold badges72 silver badges99 bronze badges asked Sep 22, 2021 at 9:37 hemanthemant 491 gold badge2 silver badges4 bronze badges 1
  • What error are you receiving? Is this the only code you are using? – ale13 Commented Sep 22, 2021 at 13:41
Add a ment  | 

2 Answers 2

Reset to default 3

Google recently updated their iframe restrictions:

In the HTML Service iframe sandbox, allow-top-navigation, which allows the content to navigate its top-level browsing context, is restricted and not set as an attribute in the sandbox. Instead, the allow-top-navigation-by-user-activation attribute has been added to the sandbox.

If you need to redirect your script, add a link or a button for the user to take action on.

The function should work, if the function is called from the onclick handler of a button. The function should be directly associated with a click by the end user.

References:

  • Html Spec: allow-top-navigation-by-user-activation (See transient activation)

I'm experiencing the same issue, in connection with google.script.run, but it happens inconsistently:

I have a button whose click handler is a function such as this

function reload() {
   google.script.run
   .withSuccessHandler(function(){
     window.open("https://someurl", "_top");
   })
   .someBackendFunction()
}

Sometimes when I click the button, the page reloads as expected, and sometimes instead I get the error

Unsafe attempt to initiate navigation for frame with origin '...'. The frame attempting navigation of the top-level window is sandboxed with the 'allow-top-navigation-by-user-activation' flag, but has no user activation (aka gesture).

Based on some experimentation, and a close reading of the HTML spec linked by TheMaster, the behavior seems to depend on the elapsed time between the user clicking the button, and the script attempting to navigate away. (In my experiment on Chrome v.116, a backend execution of more than 5 seconds always triggered the error.)

As documented here:

The transient activation duration is expected be at most a few seconds, so that the user can possibly perceive the link between an interaction with the page and the page calling the activation-gated API.

My workaround is: in the success handler callback to google.script.run, I present the user with a new button, "Reload Page," whose click handler performs the navigation. This avoids the latency between button click and page reload.

本文标签: