问题
My current project requires opening/activating popup.html (browserAction) from content script? I have read a few answers that say, it's not possible because of security concern.
E.g Ans: How can I open my extension's pop-up with JavaScript?
If that's the case, then how Chrome Bookmark Manager extension does it when CTRL+D shortcut is pressed?
Ref: Bookmark Manager Extension
回答1:
They're just using the commands interface, with an event name of _execute_page_action
(or _execute_browser_action
). They've set the default shortcut to Ctrl+D
.
Activating this command will perform a click on the page (or browser) action, opening the popup or whatever the action is configured to do.
Chrome only respects the default shortcut key if it does not conflict with built-in commands, but it makes an exception for extensions that specify chrome_ui_overrides.bookmarks_ui
[1], which is currently restricted to the dev channel or the experimental extension by Google.
The Cast extension used to programmatically open its popup, which is the only one I've seen. The API is similarly whitelisted: browserAction.openPopup.
回答2:
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/pageAction/setPopup
Looks like they are using this.
a.h.get(c).then(function(a) {
var c = chrome.pageAction;
a ? (c.setTitle({
tabId: b,
title: Ga
}),
c.setIcon({
tabId: b,
imageData: {
19: Cc,
38: Dc
}
})) : (c.setTitle({
tabId: b,
title: Ha
}),
c.setIcon({
tabId: b,
imageData: {
19: Ec,
38: Fc
}
}));
c.setPopup({
tabId: b,
popup: "popup.html"
});
c.show(b)
})
};
Looking at background_compiled.js
This looks supported but I have not tested. This me briefly looking at the code and just running a search for popup.html. But this might be what you are looking for.
来源:https://stackoverflow.com/questions/42923097/activate-popup-html-from-content-script-chrome-extension-development