Chrome扩展程序:将固定div作为UI插入

时间:2013-07-31 20:06:07

标签: javascript css dom google-chrome-extension

我想使用chrome扩展名将div插入固定位置。它将覆盖您当前正在查看的页面。我担心的是,我希望这可以在任何页面上工作而不需要更改它(除了插入我的固定div),但我不知道这是否可能与我正在做的方式。目前,按钮不会显示,我在显示div时遇到了很多麻烦。顺便说一句,定位只是暂时的,我会在页面上找到它后正确定位! :)这就是我所拥有的:

这是我的清单:

{
    "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"
    }
}

以下是将执行此功能的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 } );             

}

1 个答案:

答案 0 :(得分:19)

从background.js处理内容是一个非常糟糕的主意。 你为什么不使用内容脚本?

的manifest.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

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%';