javascript Chrome 扩展:插入固定 div 作为 UI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17979903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome extension: Insert fixed div as UI
提问by Lebowski156
I want to insert a div into a fixed position using a chrome extension. It will overlay the page that you are currently viewing. My concern is that I want this to work on any page without altering it (other than inserting my fixed div), but I don't know if that is possible with the way that I'm doing it. Currently, the button won't show up, and I had a lot of trouble getting the div to show up. By the way, the positioning is just temp for now, I will position it correctly once I get it on the page! :) Here's what I have:
我想使用 chrome 扩展将 div 插入到固定位置。它将覆盖您当前正在查看的页面。我担心的是,我希望它可以在任何页面上运行而无需更改它(除了插入我的固定 div),但我不知道我的做法是否可行。目前,该按钮不会显示,而且我在显示 div 时遇到了很多麻烦。顺便说一句,定位现在只是临时的,一旦我在页面上找到它,我就会正确定位!:) 这就是我所拥有的:
Here is my manifest:
这是我的清单:
{
"name":"poop",
"version":"0.1",
"manifest_version":2,
"description":"shitty app I'm making",
"background":{
"scripts":[
"scripts/modernizr.min.js",
"scripts/background.js"
],
"persistent": false
},
"permissions":[
"contextMenus",
"tabs",
"http://*/*",
"https://*/*"
],
"icons":{
"16":"images/icon_16.png",
"128":"images/icon_128.png"
}
}
Here is the function in background.js that will be performing this functionality:
这是 background.js 中将执行此功能的函数:
function insertUIDiv()
{
var prepHtmlStyle = "document.documentElement.style.height = '100%';" +
"document.body.style.height = '100%';" +
"document.documentElement.style.width = '100%';" +
"document.body.style.width = '100%';";
var insertDiv = "var div = document.createElement( 'div' );" +
"var btnForm = document.createElement( 'form' );" +
"var btn = document.createElement( 'input' );" +
//append all elements
"document.body.appendChild( div );" +
"div.appendChild( btnForm );" +
"btnForm.appendChild( btn );" +
//set attributes for div
"div.id = 'myDivId';" +
"div.style.position = 'fixed';" +
"div.style.top = '50%';" +
"div.style.left = '50%';" +
"div.style.width = '100%';" +
"div.style.height = '100%';" +
"div.style.backgroundColor = 'red';" +
//set attributes for btnForm
"btnForm.action = '';" +
//set attributes for btn
//"btn.removeAttribute( 'style' );" +
"btn.type = 'button';" +
"btn.value = 'hello';" +
"btn.style.position = 'absolute';" +
"btn.style.top = '50%';" +
"btn.style.left = '50%';";
chrome.tabs.executeScript( null, { code: prepHtmlStyle } );
chrome.tabs.executeScript( null, { code: insertDiv } );
}
回答by Okan Kocyigit
Manipulating content from background.js is a very bad idea. Why don't you use content script?
从 background.js 操作内容是一个非常糟糕的主意。为什么不使用内容脚本?
manifest.json
清单文件.json
{
"name":"poop",
"version":"0.1",
"manifest_version":2,
"description":"shitty app I'm making",
"background":{
"scripts":[
"scripts/modernizr.min.js",
"scripts/background.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": ["https://*/*", "http://*/*"],
"js": ["content.js"],
"run_at": "document_end"
}
],
"permissions":[
"contextMenus",
"tabs",
"http://*/*",
"https://*/*"
],
"icons":{
"16":"images/icon_16.png",
"128":"images/icon_128.png"
}
}
content.js
内容.js
document.documentElement.style.height = '100%';
document.body.style.height = '100%';
document.documentElement.style.width = '100%';
document.body.style.width = '100%';
var div = document.createElement( 'div' );
var btnForm = document.createElement( 'form' );
var btn = document.createElement( 'input' );
//append all elements
document.body.appendChild( div );
div.appendChild( btnForm );
btnForm.appendChild( btn );
//set attributes for div
div.id = 'myDivId';
div.style.position = 'fixed';
div.style.top = '50%';
div.style.left = '50%';
div.style.width = '100%';
div.style.height = '100%';
div.style.backgroundColor = 'red';
//set attributes for btnForm
btnForm.action = '';
//set attributes for btn
//"btn.removeAttribute( 'style' );
btn.type = 'button';
btn.value = 'hello';
btn.style.position = 'absolute';
btn.style.top = '50%';
btn.style.left = '50%';