【发布时间】:2013-12-06 07:01:19
【问题描述】:
我想通过使用 chrome 扩展中的内容脚本将 iFrame 添加到网页顶部。
当我这样做时的问题是:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
var iFrame = document.createElement ("iframe");
iFrame.src = chrome.extension.getURL ("chrome.html");
iFrame.setAttribute("scrolling", "no");
iFrame.setAttribute("frameborder", "0");
iFrame.setAttribute("style", "border:none; width:100%; height:20%; position: fixed;top: 10px; bottom: 10px;left: 10px;right: 10px;");
document.body.insertBefore (iFrame, document.body.firstChild);
function checkScroll()
{
//remove the iframe
//iFrame.parentNode.removeChild(iFrame);
//or remove after 2 secs
setTimeout(function(){ iFrame.parentNode.removeChild(iFrame); }, 2000);
//off the scroll listener
document.removeEventListener('scroll', checkScroll);
}
document.addEventListener('scroll', checkScroll);
就是这样,有时 iframe 在网页下方加载。我希望它从浏览器窗口的顶部加载。此外,它不会加载到谷歌搜索网页中。 我哪里错了?
【问题讨论】:
-
您尝试过我下面提出的解决方案吗?它对你有用吗?
标签: html css iframe google-chrome-extension