google-apps-scriptiframeweb-applicationsgoogle-workspace

Unable to navigate to the page after sign In


Error: Unsafe attempt to initiate navigation for frame with origin 'https://script.google.com' from frame with URL 'https://n-x4qqieogl32dh7vitgxnysfogdq2h5ccinv5p6a-0lu-script.googleusercontent.com/userCodeAppPanel'. 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). See https://www.chromestatus.com/feature/5629582019395584.

Code:

function ModelViewer(token) {
  if (!token) {
    console.log("No token found");
    return;
  }
   
      
    // Fetch the URL dynamically
  google.script.run.withSuccessHandler(function (appUrl) {
    console.log("Fetched URL:", appUrl);

  google.script.run.withSuccessHandler(function (isValid) {
    if (isValid) {
      console.log("isValid it is!")
      console.log("Token", token);
      const link = document.createElement('a');
      link.href = `${appUrl}?page=Models&token=${token}`;
      link.id = 'linkURL';
      document.body.appendChild(link);
      document.getElementById('linkURL').click();

    } else {
      console.log("isValid",isValid);
      console.log("Token", token);
      console.log("Invalid token. Redirecting to SignIn.");
      showNotification("Session expired. Please sign in again.", "error");
      }
    }).validateToken(token);
  }).getAppUrl(); // Call the server-side function to get the URL
}

Solution

  • As the sandbox name, allow-top-navigation-by-user-activation' says, you can't navigate to another page without a user interaction. The link needs to clicked by the end user with the target attribute set to the top frame.

      const link = document.createElement('a');
      link.href = `${appUrl}?page=Models&token=${token}`;
      link.id = 'linkURL';
      link.target = '_top'
      link.innerText = 'Signed in! Click Here to continue!'
      document.body.appendChild(link); //No auto click possible