简体   繁体   中英

Google Chrome Extension - URL Parameter Manipulation - JavaScript

Google Chrome Extension, JavaScript Issue

The aim is on button click the prefix '?wcmmode=edit' is appended to the end of the current url open. It's working to an extent, in that it appends the prefix, however in some cases '?wcmmode=disabled' already exists and therefore doesn't need to be added.

Example : Google.com/hello?wcmmode=edit. You can see it appends an additional '?wcmmode=edit' to the end of the url when it's not required.

Example shown in image

Here is my code:

document.getElementById("wcm-mode-disabled").addEventListener("click", handler_one);

function handler_one() {
chrome.tabs.query({
currentWindow: true,
active: true,
}, ([tab]) => {
const url = new URL(tab.url);
chrome.tabs.update({
  url: url + '?wcmmode=disabled',
});
});
};

Simple ,check the url for occurrence of ? . If found do not append else append. Can be done as follows var url = ' http://www.tyh.com?tab ';

            var newurl = url.substring(url.lastIndexOf('?')).split('-'); 

Use that newurl now . Instead of ? Put your custom string '?wcmmode=edit'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM