google-chrome-extension - 是否可以通过chrome扩展访问iframe的内容?

标签 google-chrome-extension browser google-chrome-frame

我想问是否有任何方法或扩展可以在打开包含 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/

相关文章:

c# - 是否可以在 Web 浏览器控件中使用 Chrome Frame

internet-explorer - Internet Explorer作为Mozilla Firefox或Chrome的行为

javascript - 从空 SVG 文档创建 HTML 页面的正确方法

javascript - 匿名登录持续多久?

javascript - 将标签输入保存在数组中并在重新打开时显示

browser - 远程视频轨道静音/启用的 webRTC 含义

google-chrome - 如何从 Chrome 扩展程序访问主机

jquery - 如何判断popup div是否在浏览器之外

javascript - 是否定义了跨操作系统 sleep /暂停的 setTimeout 行为?

google-chrome-frame - 如何在 IE 的谷歌浏览器框架插件中启用文件协议(protocol)