简体   繁体   English

Javascript | 链接/书签以删除 url 中的变量

[英]Javascript | Link/Bookmarklet to remove variables in url

I found this and was able to do what I initially wanted.我发现了这一点,并且能够做我最初想做的事情。 Javascript | Javascript | Link/Bookmarklet to replace current window location 链接/书签替换当前 window 位置

javascript:(function(){var loc=location.href;loc=loc.replace('gp/product','dp'); location.replace(loc)})()

Which was to change an amazon url from the product link to the dude perfect link.这是将亚马逊 url 从产品链接更改为完美链接。

It turns this url: https://www.amazon.com/gp/product/B01NBKTPTS/变成这个 url: https://www.amazon.com/gp/product/B01NBKTPTS/

into this url: https://www.amazon.com/dp/B01NBKTPTS/进入这个 url: https://www.amazon.com/dp/B01NBKTPTS/

I would like to take this a step further.我想更进一步。 Is there a way to do the above switch and then also remove the string of variables after the?有没有办法进行上述切换,然后在之后删除变量字符串? essentially cleaning up基本上清理

https://www.amazon.com/gp/product/B01NBKTPTS/?pf_rd_r=DQV2YXJP8FFKM1Q50KS9&pf_rd_p=eb347dce-a775-4231-8920-ae66bdd987f4&pf_rd_m=ATVPDKIKX0DER&pf_rd_t=Landing&pf_rd_i=16310101&pf_rd_s=merchandised-search-2&linkCode=ilv&tag=onamzbybcreat-20&ascsubtag=At_Home_Cooking_210426210002&pd_rd_i=B01NBKTPTS https://www.amazon.com/gp/product/B01NBKTPTS/?pf_rd_r=DQV2YXJP8FFKM1Q50KS9&pf_rd_p=eb347dce-a775-4231-8920-ae66bdd987f4&pf_rd_m=ATVPDKIKX0DER&pf_rd_t=Landing&pf_rd_i=16310101&pf_rd_s=merchandised-search-2&linkCode=ilv&tag=onamzbybcreat-20&ascsubtag=At_Home_Cooking_210426210002&pd_rd_i =B01NBKTPTS

to

https://www.amazon.com/dp/B01NBKTPTS/ https://www.amazon.com/dp/B01NBKTPTS/

Thanks!谢谢!

You've almost done it yourself!你自己几乎做到了!

To do the second part you can use split on your /?要执行第二部分,您可以在/?上使用split string (ie URL).字符串(即 URL)。

In our case that will give you an array with two elements: the first element stores everything BEFORE the /?在我们的例子中,它会给你一个包含两个元素的数组:第一个元素存储/?之前的所有内容。 (reference [0], that's what we can use ), and the other stores everything AFTER (reference [1], not needed for us) (参考[0],这就是我们可以使用的),而另一个存储之后的所有内容(参考 [1],我们不需要)

FYI: if there were more /?仅供参考:如果有更多/? , then split would produce an array with several elements. , 然后split将产生一个包含多个元素的数组。 Additional information .附加信息

In addition, you shouldn't forget to escape the special character / this way: \/ .此外,您不应忘记以这种方式转义特殊字符/\/

So here is the final working bookmarklet code to get the first URL part before /?所以这是最终的工作书签代码,用于在/? letters, with gd/product replaced by dp :字母,将gd/product替换为dp

javascript:(function(){
    var loc=location.href;
    loc=loc
            .split('\/?')[0]
            .replace('gp/product','dp')
            +'/';
    location.replace(loc);
 })();

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

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