【发布时间】:2018-06-08 18:07:54
【问题描述】:
我希望我的 Chrome 扩展程序在用户访问网站后加载 Javascript。但目前,只有当用户单击扩展图标并打开扩展弹出窗口时才会执行 Javascript。
我在这个Chrome extension to load the script without clicking on icon 问题中看到了答案。
我的 manifest.json 是:
{
"manifest_version": 2,
"name": "Javascript example",
"description": "Some description.",
"version": "1.1",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"storage"
],
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["popup.js"]
}
]
}
popup.js 是:
document.addEventListener('DOMContentLoaded', () => {
alert("Working");
});
popup.html 是:
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
警报对话框仅在我单击扩展图标时显示(即当 popup.html 运行时),而不是在页面加载时显示。我希望 popup.js 文件无需用户单击扩展图标即可执行。我错过了什么?
【问题讨论】:
-
在清单的
content_scripts条目中添加"run_at":"document_start"。 -
@IvánNokonoko 工作!
标签: javascript html google-chrome google-chrome-extension