views:

31

answers:

2

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 the 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's 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 is not working for me.

Thanks

A: 

Have u tried using javascripts window.location function instead? e.g:

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

If that doesn't work then it's probably a problem with your event listener I would have thought.

geoffs3310
that doesnt work. i belive the event listener is not called. may be i m missing something but dont knw what .
Kingkarter
A: 

If you want to redirect a tab you need to use:

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

You also need to check for status of the page as checkForValidUrl will be executed twice for every page:

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