Dario
Dario

Reputation: 9

How to delete part of a URL using Greasemonkey?

I'm trying to make a Greasemonkey script that passes me from this:

http://redirector/referal_ID:site#link

to this:

link

In other words, I need to delete the first part of the links that I click on, bypassing the redirector pages http://redirector/referal_ID:site# and keep only what is after the # character the link.

Note that redirector changes frequently, referal_id is always unique and different, and site# is the only constant string in all of the links.

I've tried to modify various scripts but my, next to null, knowledge of javascript foils all my attempts.

--------------------------------------------------------------------EDIT-----------------------------------------------------------------------

An example of what I need to do is to modify this:

http://firstfirst.net/identi_ref?q=Waterfox%2033.0.2%20[Mozilla%20Firefox%20de%2064%20bits]&ref=http://www.identi.li/c#https://shared.com/dhq1l9djj1?s=l

into this:

https://shared.com/dhq1l9djj1?s=l

The site where I want the script to work is http://www.identi.li/

Upvotes: 0

Views: 2049

Answers (1)

Brock Adams
Brock Adams

Reputation: 93473

The trickiest part of this is making sure the script does not fire on pages that are not redirectors. To do that, use a regex @include.

After that, it's just a matter of extracting the target site and changing the location. Here's a complete script:

// ==UserScript==
// @name        _Skip redirects
// @include     /site#http/
// @run-at      document-start
// ==/UserScript==

var targetSite = location.href.replace (/^.+?site#(http.+)$/, "$1");

//--- Use assign() for debug or replace() to keep the browser history clean.
location.assign (targetSite);
//location.replace (targetSite);

Note that the @run-at document-start is not strictly necessary, but it can shave the response time, of a redirect script, by a fair amount.

Upvotes: 1

Related Questions