脚本运行两次,一次运行在iframe外部,然后一次运行在iframe内部

时间:2019-02-19 03:07:32

标签: javascript iframe userscripts tampermonkey

我需要匹配iframe之外的内容,然后匹配iframe内的内容。然后,我需要使用这两个匹配项来执行某项操作,但似乎在执行此操作时遇到了麻烦。

基本上,当我尝试运行此脚本时,它首先在iframe外部的所有内容上运行整个脚本,然后在iframe内部的所有内容上再次运行整个脚本。

这使我每次运行脚本时都只获得匹配的一部分,但是我需要两个匹配才能起作用。有什么办法可以解决这个问题?

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-----------------------------------------------");

控制台结果:

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-----------------------------------------------

0 个答案:

没有答案
相关问题