Chrome Page Action not working

this is my manifest.json file

{ "name": "My First Extension", "version": "1.0", "description": "The first extension that I made.", "background_page": "background.html", "page_action": { "default_icon": "icon.png" }, "permissions" : [ "tabs" ] } 

This is background.html

 <html> <head> <script> // Called when the url of a tab changes. function checkForValidUrl(tabId, changeInfo, tab) { // If the letter 'page' is found in the tab URL... if (tab.url.indexOf('google') > -1) { // ... show the page action. chrome.pageAction.show(tabId); } }; // Listen for any changes to the URL of any tab. chrome.tabs.onUpdated.addListener(checkForValidUrl); chrome.pageAction.onClicked.addListener(function(tab) { tab.url = 'www.bing.com'; console.log('I am clicked'); } ); </script> </head> </html> 

when I click on the page action icon, I want to redirect the page to Bing.com, but this click event does not work for me.

thanks

+4
source share
2 answers

If you want to redirect the tab you should use:

 chrome.tabs.update(tab.id, {url: "http://www.bing.com"}); 

You also need to check the page status, since checkForValidUrl will be executed twice for each page:

 function checkForValidUrl(tabId, changeInfo, tab) { if(changeInfo.status === "loading") { //... } }); 
+3
source

Have you tried using the javascripts window.location function? eg:

window.location = "http://www.bing.com";

If this does not work, it may be a problem with your event listener, which I thought of.

-2
source

All Articles