我正在编写一个Chrome扩展程序,并具有以下代码
popup.html:
<script type='text/javascript' src='content.js'></script>
content.js:
var container = document.getElementById('viewer')
container.style['white-space'] = 'nowrap'
var node_list = document.getElementsByClassName('page')
Array.prototype.forEach.call(node_list, function(page) {
page.style['display'] = 'inline-block'
})
manifest.json:
{
"name": "horizontal_pdf",
"version": "0.0.1",
"manifest_version": 2,
"browser_action": {
"default_title": "My Title",
"default_popup": "popup.html",
"default_icon": {
"16": "icons/16x16.jpg",
"32": "icons/32x32.jpg"
}
}
}
但是,当我单击该图标并查看控制台时,我看到了错误消息:
Uncaught TypeError: Cannot read property 'style' of null
在content.js的第2行。因此,它无法找到id为的div viewer
。但是,当我查看页面的dom时,它就在那里。我究竟做错了什么?
PS:我没有在清单中指定内容脚本,因为我不希望它被自动触发,而仅当用户单击图标时才被触发。
根据Google的文档,您可以使用程序化注入。
在后台脚本中,您可以添加:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "content.js"});
});
或者,您可以在弹出脚本中调用:
chrome.tabs.executeScript(null, {file: "content.js"});
(您需要以activeTab
任何方式添加权限)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句