当标签更新时,我一直试图在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);
}

最佳答案

您是在扩展程序后台页面或弹出页面等内部创建iframe。您需要通过chrome.tabs.executeScript()或直接通过清单将整个代码传递到内容脚本:

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

关于javascript - 在chrome.tabs.onupdated监听器上插入iframe,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11009241/

10-16 20:12