简体   繁体   English

谷歌浏览器扩展 - URL 参数操作 - JavaScript

[英]Google Chrome Extension - URL Parameter Manipulation - JavaScript

Google Chrome Extension, JavaScript Issue谷歌浏览器扩展,JavaScript 问题

The aim is on button click the prefix '?wcmmode=edit' is appended to the end of the current url open.目的是单击按钮,将前缀“?wcmmode=edit”附加到当前打开的 url 的末尾。 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.它在一定程度上起作用,因为它附加了前缀,但是在某些情况下,'?wcmmode=disabled' 已经存在,因此不需要添加。

Example : Google.com/hello?wcmmode=edit.示例: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.您可以看到它在不需要时在 url 末尾附加了一个额外的 '?wcmmode=edit'。

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 ?简单,检查 url 是否出现 ? . . If found do not append else append.如果找到则不追加,否则追加。 Can be done as follows var url = ' http://www.tyh.com?tab ';可以做如下 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'把你的自定义字符串 '?wcmmode=edit'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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