在chrome.tabs.onupdated侦听器上插入iframe

时间:2012-06-13 06:20:25

标签: javascript google-chrome google-chrome-extension

我一直在尝试在更新标签时在Google页面中添加iframe。 iframe显示更新的网址和标题。程序上它显示已插入iframe但我无法在页面上看到iframe。 以下是我的内容脚本中的代码:

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { //onUpdated should fire when the selected tab is changed or a link is clicked 
  if (changeInfo.status == "complete") {
    chrome.tabs.getSelected(null, function (tab) {
        if (tab.url.indexOf(".google.") != -1) {
            url = getRetailer(tab.url);
            title = tab.title;
            title = tab.title.replace(" - Google Search", "");
            createiframe(url, title);
        }

    });
  }
});

function createIframe(url, PN, uid) {
   var iframe = document.createElement("iframe");
   var div = document.createElement("div");
   iframe.src = "url:" + url + 'title:" + PN ;
   iframe.setAttribute("id", "cr_Iframe");
   iframe.setAttribute("style", "position: fixed; z-index: 100001; left: 0pt; right: 0pt; top: 0pt; width: 100%; height: 42px; display: block;");
   iframe.setAttribute("scrolling", "no");
   iframe.setAttribute("frameborder", "0");
   iframe.setAttribute("name", "cr_Iframe");
   document.body.insertBefore(iframe, document.body.firstChild);
}

1 个答案:

答案 0 :(得分:2)

您正在扩展背景页面或弹出页面或其他内容中创建iframe。您需要通过chrome.tabs.executeScript()或直接通过清单将整个代码传递给内容脚本:

"content_scripts": [
   {
     "matches": ["*://*.google.*"],
     "js": ["body_of_createIframe_function.js"]
   }
 ],