我想问是否有任何方法或扩展可以在打开包含 iframe 的新窗口时预先突出显示 iframe 中的文本?我尝试了很多扩展,但没有一个有效。 我需要根据某些关键字过滤掉内容,并且内容位于 iframe 内。我可以用 CTRL+F 来完成,但是每篇文章中都有很多关键字,例如 10-15。所以这让我的工作变得非常艰难和耗时。我尝试过的 chrome 扩展很少有多重荧光笔、珍珠、FF,但它们似乎都不起作用。
我还知道这些扩展程序无法访问 iframe 内的内容的原因,即由于跨源策略。
但我还记得大约一年前,我使用了名为“自动填充”的 chrome 扩展,每当我打开包含 iframe 的新 chrome 窗口时,它都可以预先选择表单元素。
那么有什么解决办法吗?
最佳答案
您可以设置扩展程序权限以在所有框架中运行内容脚本作为文档 http://developer.chrome.com/extensions/content_scripts.html#registration通过在 list 文件的内容脚本部分中将 all_frames 设置为 true 。从该页面添加到 Google 的示例中, list 文件的一部分可能如下所示
{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["http://www.google.com/*"],
"css": ["mystyles.css"],
"js": ["jquery.js", "myscript.js"],
"all_frames": true
}
],
...
}
您需要小心,因为您的内容脚本将为父页面注入(inject)一次,并为页面上的每个 iFrame 注入(inject)一次。将内容脚本注入(inject)页面上的所有框架后,您就可以通过查找和突出显示文本来施展魔法。
if (window === top) {
console.log('Running inside the main document', location.href);
} else {
console.log('Running inside the frame document', location.href,
[...document.querySelectorAll('*')]);
}
关于google-chrome-extension - 是否可以通过chrome扩展访问iframe的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384784/