Chrome extension disable page action popup - google-chrome-extension

I would like to use pageAction.onClicked, The document says the event will not fire if there is a popup:
Fired when a page action icon is clicked. This event will not fire if the page action has a popup.
So I am looking for a way to disable the popup. Right now I tried something like this:
chrome.tabs.query({active: true}, function(tabs){
chrome.pageAction.setPopup({
tabId: tabs[0].id, popup: ''
});
});
I didn't specify default_pop in manifest.json.
But this doesn't disable the popup, any advice?
Related: Chrome extension button popup stuck open

Related

Not allowed to load local resource when opening chrome:// UI page from a chrome extension

I have a MV2 Chrome extension that on the popup page I added a "Shortcut" link so that user can access chrome://extensions/shortcuts by clicking it.
However, after upgrading to MV3, the link doesn't work.
Should I simply remove this feature?
You can resolve this issue by opening the page programmatically.
add some suitable selector to the link (popup html):
Configure Commands
add an event listener to open the shortcuts page (in popup script):
// get the DOM node
const link = document.getElementById("commands-link");
// add click event handler that opens the shortcuts page
link.addEventListener('click', () => chrome.tabs.create({
url: "chrome://extensions/configureCommands"
}));

gulp refresh active tab after code change in chrome extension

I'm developing a chrome extension and I want to automatically refresh the active tab after a code change
I got gulp-open to work so I don't need to navigate to chrome://extensions anymore and manually click "reload"
now only the active tab refresh remains
I've tried gulp-livereload but couldn't get it to work for developing a chrome extension
any ideas how to approach this?
Could you post the code of your chrome extension? Without much info, I hope this is helpful. You can inject the code below to refresh the page after the chrome extension is reloaded:
chrome.tabs.query({
active: true, // Select active tabs
lastFocusedWindow: true // In the current window
}, function(tabs) {
// Since there can only be one active tab in one active window,
// the array has only one element
var tab = tabs[0];
// Javascript to reload the page
var code = 'window.location.reload();';
// Execute the code for the current tab
chrome.tabs.executeScript(tab.id, {code: code});
});

Message listener only begins firing after browserAction has been clicked

It appears that a message from content to background only begins firing after backgroundAction has been run at least once.
In the below code example, a click on browserAction turns the page red, and a click on the page body turns the page blue (via a message sent by the content script).
If I click the page body first, nothing happens. It only begins working after I've clicked browserAction once.
Why is this happening and how can I make it so the message listener fires without having browserAction run first?
Any help would be greatly appreciated!
content.js
$(function(){
$('body').on('click', function() {
// Send a message to background.js
chrome.runtime.sendMessage(true, function(response){
console.log(response);
});
});
});
background.js
// Make background red when browserAction is cliked
chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.executeScript( {
code: 'document.body.style.backgroundColor="red"'
});
});
// Make background blue when any message is received
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse){
chrome.tabs.executeScript( {
code: 'document.body.style.backgroundColor="blue"'
});
return true;
})
As always in such cases use the debugger. The error I'm seeing here in the extension's background page console which can be opened on chrome://extensions page:
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "...". Extension manifest must request permission to access this host.
When runtime.onMessage is executed after a message from the content script Chrome doesn't know that executeScript was initiated by a user action so the code is blocked.
As for browserAction.onClicked, it is always invoked on user's interaction so "permissions": ["activeTab"] is sufficient for the code executed in the eventjob context of the issued click. And it creates temporary permission to modify the active tab, see the documentation:
The following user gestures enable activeTab:
Executing a browser action
Executing a page action
Executing a context menu item
Executing a keyboard shortcut from the commands API
Accepting a suggestion from the omnibox API
Solution #1 (the best) would be to avoid injecting any code from the background script and do everything in the content script based on messages from the background script.
Solution #2 would be to add the required permission to manifest.json:
"permissions": ["activeTab", "<all_urls>"]

Chrome extension : how to detect click in background process?

I am devlopping a google chrome extension
I have a background process and a popup window.
I want to not use the popup window any more, but instead open a web browser window when chrome extension button is clicked:
How do I do that : How do I detect the click inside background.js ?
This is quite easy
chrome.browserAction.onClicked.addListener(function(tab) {
// The action icon has been clicked
});
You'll have to declare the extension as a browser action in your manifest and remove the popup option

Chrome extension browserAction.onClicked not listening

I have the following in my background.js file which is referenced in manifest.json:
chrome.browserAction.onClicked.addListener(function (tab) {
console.log('browserAction clicked');
});
When I click on the browser action icon, I never see browserAction clicked printed to the console.
Am I misunderstanding how this is supposed to work?
Console.log doesn't work in background.js. Use
chrome.browserAction.onClicked.addListener(function (tab) {
alert('browserAction clicked');
});
instead of that.
To see the console messages that comes from the background.js
display the extension;
chrome://extensions/.
Make sure that developer mode is enabled
Click on the "Inspect Views"
of your background page
You should see console messages under the
console tab.
Does the browser action have a popup? If so, the event won't fire.

Resources