use*_*366 1 google-chrome-extension
我想念没有显示弹出窗口吗?
在扩展文件夹中,我有四个文件。我将该文件夹加载为扩展名。我可以看到该图标,但是单击它时不会显示弹出窗口。为什么?
content_script.js
:空(只是添加以便我可以加载扩展名)icon.png
:显示在我加载扩展程序时。manifest.json
:{
"name": "Popup poc",
"version": "1.4",
"description": "Simple popup example",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content_script.js"]
}
],
"page_action": {
"default_name": "Display Map",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)
popup.html
:<!doctype html>
<html>
<head>
<title>Popup</title>
</head>
<body>
This is body
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在您的电脑page_action
中用“ browser_action
” 替换“ ” manifest.json
。这样,您应该可以在所有页面上看到弹出窗口。
可能与以下项重复:Chrome扩展程序弹出窗口不再显示, 但似乎我没有足够的声誉点对此进行标记。
(编辑)
如果希望仅在某些网站(例如,仅在Google)上可以使用弹出窗口,则需要declarativeContent
在清单中指定权限,并在后台脚本中进行一些设置。
因此,您manifest.json
将看起来像这样:
{
"name": "Popup poc",
"version": "1.4",
"description": "Simple popup example",
"permissions": ["declarativeContent"],
"content_scripts": [
{
"matches": ["https://www.google.com/*"],
"js": ["content_script.js"]
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"page_action": {
"default_name": "Display Map",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)
您background.js
将如下所示:
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [new chrome.declarativeContent.PageStateMatcher({
pageUrl: {hostEquals: 'www.google.com'},
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
});
Run Code Online (Sandbox Code Playgroud)
该代码主要来自“ 入门指南”。
归档时间: |
|
查看次数: |
195 次 |
最近记录: |