我正在构建Chrome扩展程序,并希望在用户单击Chrome中的浏览器操作图标时显示弹出窗口并运行脚本。
我可以通过在manifest.json中放置'“ default_popup”:“ popup.html”'来使弹出窗口发生。但是,当我这样做时,background.js似乎没有运行。当我删除'“ default_popup”:“ popup.html”'时,background.js似乎正在运行。
如何同时显示弹出窗口和脚本?
manifest.json
{ "manifest_version": 2, "name": "Typo Blaster", "description": "Blast away typos and make the internet a safer place for the kids.", "version": "0.1", "browser_action": { "default_icon": "icon.png", "default_title": "Blast the typo!", "default_popup": "popup.html" }, "permissions": [ "activeTab" ], "background": { "scripts": ["background.js"], "persistent": false }, "icons": { "16": "icon16.png", "48": "icon48.png", "128": "icon128.png" } }
popup.html
Typo Blaster Got it!
background.js
chrome.browserAction.onClicked.addListener(function(tab) { // No tabs or host permissions needed! console.log('Turning ' + tab.url + ' red!'); chrome.tabs.executeScript({ code: 'document.body.style.backgroundColor="red"' }); }); alert('hello ' + document.location.href);
Daniel.. 5
onClicked
有弹出页面时,不能使用该方法。Google文档。
onClicked:单击浏览器操作图标时触发。如果浏览器操作弹出,则不会触发此事件。
如果您想同时保留两者,请考虑编写内容脚本。