-1

I have the following html code:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

    <h1>Soemething</h1>
    <button id='btn'>Take me to Google</button>

    <script>

      document.getElementById('btn').addEventListener('click', doStuff);

      function doStuff() {
        window.location.assign("https://www.google.com/");
      }

    </script>

  </body>
</html>

How do I wait for Google to load before executing code on google.com such as doing a search?

Thank you

Monis
  • 165
  • 2
  • 12
user688003
  • 23
  • 4
  • Not possible without having special permissions on the client's browser, or controlling google yourself – CertainPerformance Feb 24 '19 at 07:05
  • Your script's sandbox is only the page whether it is executing. Once, you are redirected to google.com, your script will become invalid. Cross-domain scripting is not allowed. – Kartik Chauhan Feb 24 '19 at 07:08
  • What functionality are you trying to achieve? If you want to provide an overlay for Google, you might try [UserScripts](https://en.wikipedia.org/wiki/Userscript) or [Web Extensions](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions). Otherwise you can try [launching it in an iFrame](https://stackoverflow.com/questions/251420/invoking-javascript-code-in-an-iframe-from-the-parent-page) but browsers will restrict your ability to access the internals. – rovyko Feb 24 '19 at 07:13

1 Answers1

0

Your script's sandbox is only the page whether it is executing. Once, you are redirected to google.com, your script will become invalid. Your only option is to get the page via AJAX and run a callback function to execute when the content is loaded.

Kartik Chauhan
  • 2,779
  • 5
  • 28
  • 39
  • 1
    That isn't an option -- Google doesn't have a cross-domain policy to permit that. –  Feb 24 '19 at 07:14