1

I am currently using the following code to add a param to a current tab URL:

chrome.browserAction.onClicked.addListener(function(tab) { 
chrome.tabs.update(tab.id, {url: tab.url + '?example=false'});
});

Actual Result: Param can be added multiple times to a URL

Required Result: Param can only be added once, when already present an alert is displayed.

Jhd33
  • 74
  • 5
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Isaac Jan 15 '18 at 20:42

1 Answers1

2

You can easily achieve this try below code:

chrome.browserAction.onClicked.addListener(function(tab) { 
    var current_url = tab.url;
    if( current_url.indexOf('example=false') < 0 ) {
        chrome.tabs.update(tab.id, {url: current_url + '?example=false'});
    } else {
        alert('Param already added.');
    }
});

Hope this will help you.

Aefits
  • 3,399
  • 6
  • 28
  • 46