使用chrome.tabs.insertCSS时,即使主机页未应用任何CSS,也不会应用我的CSS规则
对于chrome扩展,我试图对通过脚本注入的某些元素应用一组CSS规则。我尝试单独运行注入的html,显式链接CSS文件,在这种情况下它可以工作,但没有任何一种工作方式。下面是代码 my manifest.json中的代码段:使用chrome.tabs.insertCSS时,即使主机页未应用任何CSS,也不会应用我的CSS规则,css,google-chrome,google-chrome-extension,Css,Google Chrome,Google Chrome Extension,对于chrome扩展,我试图对通过脚本注入的某些元素应用一组CSS规则。我尝试单独运行注入的html,显式链接CSS文件,在这种情况下它可以工作,但没有任何一种工作方式。下面是代码 my manifest.json中的代码段: "background": { "scripts": [ "Scripts/Background/background.js" ] }, "browser_action": { "default_icon": "img/icon.png"
"background": {
"scripts": [ "Scripts/Background/background.js" ]
},
"browser_action": {
"default_icon": "img/icon.png"
//"default_popup": "popup.html"
},
"permissions": [
"bookmarks",
"tabs",
"http://*/*",
"https://*/*",
"activeTab"
],
"web_accessible_resources": [
"HTML/popup.html", "Scripts/External/jquery-2.1.3.min.js", "Scripts/Injected/loadExtUI.js", "Style/extUI.css"
]
我的背景脚本:
// Supposed to be Called when the user clicks on the browser action icon.
function loadExt() {
chrome.tabs.insertCSS(null, { file: "Style/extUI.css" }, function () {
alert("Inserted CSS") //This alert works
});
chrome.tabs.executeScript(null, { file: "Scripts/External/jquery-2.1.3.min.js" }, function () {
chrome.tabs.executeScript(null, { file: "Scripts/Injected/loadExtUI.js" });
});
}
chrome.browserAction.onClicked.addListener(loadExt);
脚本/Injected/loadExtUI.js:
var popup_root = document.getElementById('chimp_ext_container');
if (popup_root == null) {
$.get(chrome.extension.getURL("HTML/popup.html"), function (data) {
//$(data).appendTo('body');
// Or if you're using jQuery 1.8+:
$($.parseHTML(data)).appendTo('body');
});
} else {
document.body.removeChild(popup_root);
}
Style/extUI.css
#chimp_ext_container {
max-width: 420px !important;
overflow-x: hidden !important;
font-family: Arial, sans-serif !important;
font-size: 12px !important;
margin:1px !important;
padding:10px !important;
border: 1px groove !important;
border-radius: 5px !important;
background-color:aqua;
position: absolute !important;
top:10px !important;
float:right !important;
}
最后,这里是HTML/pop
<!doctype html>
<html>
<head>
<title>Chimpu</title>
</head>
<body>
<div id="chimp_ext_container">
<h1>Chimpu mera doshtttt!</h1>
</div>
</body>
</html>
黑猩猩
黑猩猩梅拉·多希特!
我正在运行的测试页面是我自己的页面,它只有一个元素,没有应用其他CSS,所以我确信,没有任何东西会干扰我的CSS文件
在调试时,我注意到应用于
的css样式可以被正确地看到,但不适用于
你的代码在我这边运行得很好。我可以看到文字“Chimpu mera Doshttt!”的颜色为“aqua”,大小合适。你可以通过重新加载测试页面来再次测试它吗?我尝试刷新页面,重新加载扩展,但没有任何帮助。令人惊讶的是,它为你运行!在文件://
方案上是否有“您自己的页面”?没有。它在本地主机上运行