gpt4 book ai didi

javascript - Chrome 扩展 : Insert fixed div as UI

转载 作者:可可西里 更新时间:2023-11-01 02:33:16 24 4
gpt4 key购买 nike

我想使用 chrome 扩展程序将一个 div 插入到固定位置。它将覆盖您当前正在查看的页面。我担心的是我希望它可以在任何页面上工作而不改变它(除了插入我的固定 div),但我不知道我这样做是否可行。目前,该按钮不会显示,我在让 div 显示时遇到了很多麻烦。顺便说一句,定位暂时只是临时的,一旦我在页面上得到它,我会正确定位它! :) 这是我所拥有的:

这是我的 list :

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

}

最佳答案

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

list .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"
}
}

内容.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%';

关于javascript - Chrome 扩展 : Insert fixed div as UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17979903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com