【发布时间】:2014-09-22 10:29:54
【问题描述】:
所以我正在制作一个 chrome 扩展程序,当您将鼠标悬停在指向它的超链接上时,它可以有效地为您提供网页预览。
我通过创建一个 div 并用各种项目填充它来做到这一点。问题是由于某种原因,我指定的 css 样式表没有被注入到 div 中,而是 div 采用了其父级(您当前所在的网站)的样式,这是有问题的,因为我无法控制外观.
这是我尝试注入 css 的清单:
{
"manifest_version": 2,
"name": "Link Preview",
"description": "This extension provides a preview of a hyperlinks destination on hover action.",
"version": "1.0",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["jquery-1.11.1.min.js","core.js"],
"css" : ["style.css"],
"run_at":"document_end",
"all_frames": false
}
],
"browser_action": {
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"<all_urls>"
],
"web_accessible_resources": ["index.html","style.css"]
}
这是包含元素的 html 文件。如果我将样式应用于 html 中的项目,它会起作用,但老实说,这样做很痛苦。
<div id="icon_wrapper">
<img id="icon_img"
src="https://i.imgur.com/mLAiR8t.png"
alt="Webpage Icon" >
</div>
<div id="title_wrapper" >
<h1 id="title_string">
testing</h1>
</div>
<div id="description_wrapper">
<h1>description</h1>
</div>
这是无法注入的 CSS,我什至尝试设置 h1 而不是 title_string 的样式,但也没有用:
#title_string {
background-color: #ffffff !important;
margin: 3px !important;
padding: 3px !important;
font-family: "Times New Roman", Times, serif !important;
color: #a48938 !important;
}
非常感谢任何帮助,我一直在努力解决这个问题
【问题讨论】:
-
嗯,这不是一个完整的答案,但你需要看看 Shadow DOM 的方向。以下是关于该主题的几个相关问题:stackoverflow.com/questions/12783217/…stackoverflow.com/questions/20725770/…
-
您的清单列出了两个不同的 html 文件。我想
index.html是你给我们的?你能告诉我们你是怎么注射的吗?您的清单列出了style.css两次。内容脚本样式应该不是必需的。
标签: html css google-chrome google-chrome-extension stylesheet