有没有一种方法可以根据IFRAME中的文本字符串生成IF语句



目前,我有一个网页嵌入了以下IFRAME:

<iframe src="https://docs.google.com/spreadsheets/u/0/d/1P9DhWOHcl14Y7-P5wCxTm-sUceckGquPoOobO75XhvM/htmlembed/sheet?gid=630924986&single=true" frameborder="0" width="393px" height="385px" style="float:centre" id="MyFrame"></iframe>

单元格的内容将根据公式进行更改。我正试图找到一种方法来检查其中一个单元格(在本例中,包含"Foo"的单元格以类"S2"列出(请参见此处((是否包含字符串。根据结果,它会将不同的内容打印到控制台。例如:

if <"Cell from inside the IFRAME with class S2"> = "stringtosearch"
//found
console.log("The cell contains stringtosearch!")
else
//not found
console.log("The cell does not contain stringtosearch. :( ")

如有任何帮助,我们将不胜感激!我相信jQuery可以做到这一点,但我不确定最好的方法。我还是JS的新手。

注意:我需要将其嵌入到Chrome扩展中,因此我不能使用提取Google Sheets API并以这种方式搜索它的选项,因为Chrome扩展不允许内联脚本

谢谢!

就像@DarrenH在评论中回避的那样,你有跨域iframe的问题。如果你的iframe在同一个域上,你可以提取tabletd,如下所示:

jQuery:

var filteredContents = $('#MyFrame').contents().find('.s2');
if (filteredContents.text() == 'stringtosearch') {
//found
console.log("The cell contains stringtosearch!")
}
else {
//not found
console.log("The cell does not contain stringtosearch. :( ")
}

香草JavaScript:

var iframe = document.getElementById("MyFrame");
var element = iframe.contentWindow.document.getElementsByTagName("td");
for (int i = 0; i < element.length; i++) {
if (element[i].classList.contains('s2')) {
if (element[i].innerText == 'stringtosearch') {
//found
console.log("The cell contains stringtosearch!")
}
else {
//not found
console.log("The cell does not contain stringtosearch. :( ")
}
}
}

最新更新