Chrome扩展程序内容脚本和iframe

ric*_*res 12 javascript google-chrome-extension

我试着为谷歌浏览器制作一个扩展程序:

加载www.example.com时运行example.com有一个iframe到另一个网站,我需要访问这个iframe的链接(这是免费的,我需要抓取下载链接)

到目前为止这么好,但..

然后我需要扩展名来通知链接url到example.com进行进一步处理.

任何想法o方向?

我已阅读http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication但无法使其正常工作...

ser*_*erg 38

您需要注入2个内容脚本:

"content_scripts": [
    {
      "matches": ["http://www.example.com/*"],
      "js": ["example.js"]
    },{
      "matches": ["http://www.rapidshare.com/*"],
      "all_frames": true,
      "js": ["rapidshare.js"]
    }
]
Run Code Online (Sandbox Code Playgroud)

要将链接从一个脚本转移到另一个脚本,您需要通过后台页面进行通信:

rapidshare.js:

chrome.extension.sendRequest({url: "link"});
Run Code Online (Sandbox Code Playgroud)

background.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    chrome.tabs.sendRequest(sender.tab.id, request);
    sendResponse({});
});
Run Code Online (Sandbox Code Playgroud)

example.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    console.log("received url:", request.url);
    sendResponse({});
});
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢您指出可以通过matches属性将不同的脚本加载到不同的页面中! (3认同)