Javascript 在chrome.tabs.on更新的侦听器上插入iframe
当标签被更新时,我一直试图在google页面中添加iframe。iframe显示更新的url和标题。从程序上看,它显示已插入iframe,但我无法在页面上看到iframe。 以下是我的内容脚本中的代码:Javascript 在chrome.tabs.on更新的侦听器上插入iframe,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,当标签被更新时,我一直试图在google页面中添加iframe。iframe显示更新的url和标题。从程序上看,它显示已插入iframe,但我无法在页面上看到iframe。 以下是我的内容脚本中的代码: chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { //onUpdated should fire when the selected tab is changed or a link is clicked
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"]
}
],
您是否已在manifest.json中为正在使用的url添加了所需的权限?控制台中是否有任何错误。我必须在manifest.json中添加什么权限?我已经将权限添加为“权限”:[“http://*/”、“https://*/”、“tabs”]是否还需要其他权限。不,仅此而已。控制台上是否有任何错误?@AshwinMendon不客气。如果你觉得这个答案有用的话,你至少应该接受并投票支持它。它将帮助你在将来得到更多的帮助