Bookmarklet to remove text after ?

General discussion about web technology.
Post Reply
User avatar
therube
Ambassador
Posts: 7426
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Bookmarklet to remove text after ?

Post by therube » Sun Sep 08, 2019 3:00 pm

Bookmarklet to remove the text after a ? (question mark, or some character, for that matter) from links (rather then URL in URLbar) from a web page.

Code: Select all

https://addons.mozilla.org/firefox/downloads/file/3383315/noscript_security_suite-11.0.3-an+fx.xpi?src=featured

https://addons.palemoon.org/?component=download&id=AdvancedNightMode@Off.JustOff&version=1.0.9&hash=8659729bbf0142077b866646352b9198ea30d85dec811007b23b283f5b7fa236
Becomes:

Code: Select all

https://addons.mozilla.org/firefox/downloads/file/3383315/noscript_security_suite-11.0.3-an+fx.xpi

https://addons.palemoon.org/
(The second, broken, understandably. But since you're the one causing it, you know it.)


For the URLbar, this looks to work:

Code: Select all

javascript:location.href = location.href.substring(0,location.href.indexOf("?"));

But from "links" within a webpage?


A "remove redirects" bookmarklet already does something along those lines.

Code: Select all

javascript:(function(){var%20k,x,t,i,j,p;%20for(k=0;x=document.links[k];k++){t=x.href.replace(/[%]3A/ig,':').replace(/[%]2f/ig,'/');i=t.lastIndexOf('http');if(i>0){%20t=t.substring(i);%20j=t.indexOf('&');;%20j=t.indexOf('?');%20if(j>0)t=t.substring(0,j);%20p=/https?\:\/\/[^\s]*[^.,;'">\s\)\]]/.exec(unescape(t));%20if(p)%20x.href=p[0];%20}%20else%20if%20(x.onmouseover&&x.onmouseout){x.onmouseover();%20if%20(window.status%20&&%20window.status.indexOf('://')!=-1)x.href=window.status;%20x.onmouseout();%20}%20x.onmouseover=null;%20x.onmouseout=null;%20}})();
I had long known that remove redirects was "keying in" on certain items; like &, t.indexOf('&'), & I could have sworn I tried s/&/?/, & as I'm writing this, I'm thinking... let me try again, doing just that, & it partially does what I wanted.

And then if I simply append instead of replacing: ;%20j=t.indexOf('&');%20j=t.indexOf('?'); ...


Sometimes it just helps to walk away, then come back & write things out :-).
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.5

User avatar
therube
Ambassador
Posts: 7426
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Bookmarklet to remove text after ?

Post by therube » Sun Sep 08, 2019 3:48 pm

Potentially crap, nsfw, who knows what if you "run" the links below (from the redirectors).

---

questionmark_test.html:

Code: Select all

<html>
<br>
<a
                    href="https://forums.informaction.com/viewtopic.php?f=18&amp;t=25716">href="https://forums.informaction.com/viewtopic.php?f=18&amp;t=25716"</a><br>
<br>
<a
href="http://gotozin.com/?https://forums.informaction.com/viewtopic.php?f=18&amp;t=25716">href="https://forums.informaction.com/viewtopic.php?f=18&amp;t=25716"</a><br>
<br>
<a
href="https://url.rw/?https%3A%2F%2Fforums.informaction.com%2Fviewtopic.php?f%3D18%26t%3D25716">https://url.rw/?https%3A%2F%2Fforums.informaction.com%2Fviewtopic.php%3Ff%3D18%26t%3D25716</a><br>
<br>
</html>

Partially.


It works, if there is a preceding ?.

Code: Select all

http://gotozin.com/?https://forums.informaction.com/viewtopic.php?f=18&t=25716
Becomes:

Code: Select all

https://forums.informaction.com/viewtopic.php
But if there is no preceding ?, then there is no change, the the trailing ? is not removed?

Code: Select all

https://forums.informaction.com/viewtopic.php?f=18&t=25716

How to get the trailing ? to truncate if there is no preceding ? ?
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.5

barbaz
Senior Member
Posts: 9180
Joined: Sat Aug 03, 2013 5:45 pm

Re: Bookmarklet to remove text after ?

Post by barbaz » Sun Sep 08, 2019 6:31 pm

What would be the point of such bookmarklet? Isn't this just going to break websites, like it would this forum?
*Always* check the changelogs BEFORE updating that important software!
-

User avatar
therube
Ambassador
Posts: 7426
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Bookmarklet to remove text after ?

Post by therube » Sun Sep 08, 2019 10:14 pm

In the sample, questionmark_test.html, yes.

But elsewhere it can be useful.

Could be anything tacked on the end of a url.
Might force a download, where you'd rather not, ?download.
Might set a particular time (in playback of a clip) ?t=60 (i.e. start at the 60 second mark).
Might contain tracking information that you'd like stripped out...
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.5

barbaz
Senior Member
Posts: 9180
Joined: Sat Aug 03, 2013 5:45 pm

Re: Bookmarklet to remove text after ?

Post by barbaz » Mon Sep 09, 2019 12:28 am

Try this -

Code: Select all

javascript:%28%66%75%6E%63%74%69%6F%6E%28%29%7B%66%6F%72%28%6C%65%74%20%61%20%6F%66%20%64%6F%63%75%6D%65%6E%74%2E%67%65%74%45%6C%65%6D%65%6E%74%73%42%79%54%61%67%4E%61%6D%65%28%27%61%27%29%29%7B%6C%65%74%20%75%3D%6E%65%77%20%55%52%4C%28%61%2E%68%72%65%66%2C%77%69%6E%64%6F%77%2E%6C%6F%63%61%74%69%6F%6E%2E%68%72%65%66%29%3B%75%2E%73%65%61%72%63%68%3D%27%27%3B%61%2E%68%72%65%66%3D%75%2E%68%72%65%66%3B%7D%7D%29%28%29%3B
source code:

Code: Select all

(function() {
  for (let a of document.getElementsByTagName('a')) {
    let u = new URL(a.href, window.location.href);
    u.search = '';
    a.href = u.href;
  }
})();
*Always* check the changelogs BEFORE updating that important software!
-

User avatar
therube
Ambassador
Posts: 7426
Joined: Thu Mar 19, 2009 4:17 pm
Location: Maryland USA

Re: Bookmarklet to remove text after ?

Post by therube » Wed Sep 11, 2019 3:14 pm

(That works... I'll have to have to look it over, ask, when I get some time.)
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko/20110420 SeaMonkey/2.0.14 Pinball NoScript FlashGot AdblockPlus
Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 SeaMonkey/2.49.5

Post Reply