0

I need to match something outside of an iframe, and then something inside the iframe. Then I need to use these two matches to do something, but it seems I'm having trouble doing this.

Basically, when I tried to run this script, it first runs the entire script on everything outside of the iframe, and then it runs the entire script again on everything inside the iframe.

This results in me only getting one part of the match every time the script is ran, but I need both matches for this to work. Is there any way I can fix this?

console.log("this is the start-----------------------------------------------");

var insideIframe;
var outsideIframe;

html = document.getElementsByTagName('html')[0];
text = html.innerHTML;

matches = text.match(/match the things outside the iframe/);
if (matches) {
outsideIframe = matches[0];
} //end of if (matches)
console.log(outsideIframe);

matches = text.match(/match the things inside the iframe/);
if (matches) {
insideIframe = matches[0]; 
} //end of if (matches) 
console.log(insideIframe);

console.log("this is the end-----------------------------------------------");

Results in console:

this is the start-----------------------------------------------
match the things outside the iframe
null
this is the end-----------------------------------------------

this is the start-----------------------------------------------
null
match the things inside the iframe
this is the end-----------------------------------------------
frosty
  • 2,559
  • 8
  • 37
  • 73
  • Is your `@match` matching both the URL of the parent page and the URL of the child iframe? (if so, that's why you see it running twice.) Also, `innerHTML` will not retrieve text inside another document (like an iframe) - is the child iframe on the same domain? – CertainPerformance Feb 19 '19 at 03:08
  • @CertainPerformance Yeah it's matching both. I need it to because I need something from both outside the iframe, and inside the iframe. I got both matches, but it's just at different run of the script. So the first run, it gets the match on the outside of the iframe, and didn't get the match on the inside of the iframe. The second run, is vice versa. But I need both matches, that's the problem. – frosty Feb 19 '19 at 03:15
  • Are both URLs on the same domain? – CertainPerformance Feb 19 '19 at 03:16
  • @CertainPerformance No but I used "include" so it works – frosty Feb 19 '19 at 03:18
  • at sign include – frosty Feb 19 '19 at 03:19
  • @CertainPerformance It works as in I'm able to get both matches, but at different run of the script. First run, it matches outside the iframe, and second run it matches inside the iframe. But both runs, when it matches inside the iframe, it doesn't match outside the iframe, and when it match outside the iframe, it doesn't match inside the iframe. – frosty Feb 19 '19 at 03:24
  • I was writing my own script to figure it out but realized I was coming up with something nearly identical to an answer already on here, see the linked question (in short, just requiring the two URLs isn't enough, use `postMessage` instead) – CertainPerformance Feb 19 '19 at 03:41
  • @CertainPerformance I tried reading it, but it seems too complicate for me to currently grasp. Is there something simpler to use, like some kind of session that can pass between different urls? – frosty Feb 19 '19 at 03:56
  • Yeah, that first link's answer was complicated due to showing how to work around a Chrome bug, which [doesn't appear to be necessary anymore](https://bugs.chromium.org/p/chromium/issues/detail?id=20773), so check [this one](https://stackoverflow.com/questions/11486256/how-to-get-an-ajax-get-request-to-wait-for-the-page-to-be-rendered-before-return/11489451#11489451) instead, it's a lot simpler. You might also be able to use `GM_getValue` / `GM_setValue`, which would be even simpler than that. – CertainPerformance Feb 19 '19 at 04:02
  • @CertainPerformance I'm trying to use the GM.setValue( "test", "test") console.log(GM.getValue("test")); but when I tried the console gave me GM.setValue is not a function – frosty Feb 19 '19 at 04:16
  • @CertainPerformance This website said it's a period, and I read a some post on stackoverflow that the underscore is outdated. I tried the underscore one as well, but it also doesn't work: https://wiki.greasespot.net/GM.setValue – frosty Feb 19 '19 at 04:32
  • Oh, you're right, oops! I'm always using `GM_setValue` in Tampermonkey, didn't realize its syntax was different in GM. Make sure to [@grant](https://wiki.greasespot.net/@grant) it first. – CertainPerformance Feb 19 '19 at 04:35

0 Answers0