1

in my background.js I am using the following code to to have my pageAction extension show up on Amazon.com

function checkForValidUrl(tabId, changeInfo, tab) {
  if (tab.url.indexOf('amazon.com') > -1) {
    chrome.pageAction.show(tabId)   
    chrome.pageAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {file: "bookmarklet.js"})
});
}
};
chrome.tabs.onUpdated.addListener(checkForValidUrl);

How would I changed the code so that the extesion will show up on both amazon.com and google.com pages?

Thanks.

hubs
  • 25
  • 3

1 Answers1

0
  • You need to setup chrome.pageAction.onClicked.addListener handler only once, you will have troubles with such code.
  • url member of changeInfo is optional and may not exist in some cases
  • you check for amazon may fail in some cases. e.g. for url http://google.co.uk?q=amazon.com. Use regular expression to parse url first. This is one of possible solutions

sample code to parse url

var host = parseURL(tab.url).host;
if (host.indexOf("amazon.com") >= 0 || host.indexOf(".google.") >= 0) {
  // handle these pages
}
Community
  • 1
  • 1
Andrey
  • 722
  • 2
  • 8
  • 17
  • the first addListener waits for the onClick, the second checks to make sure that the url hasn't changed or the tab hasn't closed. Both are necessary. The check does not fail for http://google.co.uk?q=amazon.com. Is regex the only way to add url.indexOf variables? – hubs Sep 20 '13 at 14:11
  • Also, my problem isn't with parsing a URL to its root, that won't effect me so much. My problem is with adding multiple URL to the tab.url.indexOf variables. – hubs Sep 20 '13 at 14:19
  • Your code will add onClicked listener every time shows page action icon. So if user opens two amazon pages then your handler will be called twice. You need to get host from the url then compare it with supported domains. I've added some sample code to the answer – Andrey Sep 20 '13 at 17:29