0

I am currently writing a userscript for website A to access another the contents on website B. So I tried to use the GM_xmlhttpRequest to do it. However, a variable on B is written to the window property eg: window.var or responseContent.var.

However, when I tried to get the window.var, the output is undefined, which means the properties under the window variable cannot be read successfully. I guess the window object is refering to the website A but not website B, so the result is undefined (There is no window.var on A).

I am sure that the GM_xmlhttpRequest has successfully read the content of the website B because I have added console.log to see the response.responseText. I have also used the window.var to successfully visit that variable on website B by browser directly.

GM_xmlhttpRequest({
            method: "GET",
            url: url,
            headers: {
              referrer: "https://A.com"
            },
            onload: function (response) {
              // console.log(response.responseText);
              let responseContent = new Document();
              responseContent = new DOMParser().parseFromString(response.responseText, "text/html");
              let titleDiv = responseContent.querySelector("title");
              if (titleDiv != null) {
                if (titleDiv.innerText.includes("404")) {
                  console.log("404");
                  return;
                } else {
                  console.log(responseContent.var);
                  console.log(window.var);
                }
              }
            },
            onerror: function (e) {
              console.log(e);
            }
          });

I would like to retrieve content window.var on website B and show it on the console.log of A

Please help me solve the problem. Thank you in advance.

Hyper
  • 1
  • 1
  • 2
  • 1
    GM_xmlhttpRequest doesn't run scripts so there's no JavaScript variables for you. Depending on the site you'll have to use an iframe and another instance of your userscript, or see if that value is somewhere in plain text in the html source so you can extract it using a regexp. – wOxxOm Aug 15 '19 at 03:52
  • @wOxxOm Could you pls give me an example or elaborate more for this: "use an iframe and another instance of your userscript". Thank you. – Hyper Aug 15 '19 at 05:25
  • There should be examples around, [here's one](/a/11774184). In your case the iframe (pointing to another site) is created by your main userscript instance. – wOxxOm Aug 15 '19 at 05:40

1 Answers1

0

@wOxxOm's comments are on point. You cannot really get the executed javascript of another website like that. One way to go around it is to use <iframe> and post message, just like @wOxxOm said. But that may fail if the other website has policy against iframes.

If this userscript is just for your use, another way is to have two scripts, one for each website and have them both open in browser tabs. Then again you can use postMessage to have those two scripts communicate the information. Dirty solution for your second userscript would be to just post the variable info on regular interval:

// Userscript for website-b.com
// needs @grant for unsafe-window to get the window.var
setInterval(()=>{postMessage(unsafeWindow.var, "website-a.com");}, 1000);

That would send an update of var's value every second. It's not very elegant, but it's simple and works. For a more elegant solution, you may want to first postMessage from website a that will trigger postMessage(unsafeWindow.var, "website-a.com"). Working with that further, you will soon find yourself inventing an asynchronous communication protocol.

Alternatively, if the second website is simple, you can try to parse the value of var directly from HTML, or wherever the value is coming from. That's a preferred solution, but requires reverse-engineering on your part.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Thank you. I finally solved the problem by extracting the html elements and re-parse them to get the data. It's the easiest way to get rid of the problem. – Hyper Aug 16 '19 at 04:33