2012-06-13 23 views
1

我一直試圖在更新選項卡時在iframe中添加iframe。 iframe顯示更新的網址和標題。從程序上來說,它表明iframe已被插入,但我無法看到頁面上的iframe。 以下是我的內容腳本代碼:在chrome.tabs.onupdated偵聽器上插入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); 
} 
+0

您已經添加在manifest.json中爲您正在使用的URL所需的權限?控制檯中是否有錯誤? – user1092042

+0

我必須在manifest.json中添加什麼權限?我已經將權限添加爲「權限」:[「http:// * /」,「https:// * /」,「tabs」]是否還有其他要求。 –

+0

沒有關於它的問題。控制檯上是否有任何錯誤? – user1092042

回答

2

你正在創建內部分機背景頁或彈出頁面或任何iframe中。你需要整個代碼傳遞到內容腳本,或者通過chrome.tabs.executeScript()或直接通過清單:

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

謝謝grigore ..它確實是一個很好的幫助 –

+0

@AshwinMendon不客氣。如果您覺得它有用,您還應至少接受&upvote這個答案。它會幫助你在將來獲得更多的幫助 –