2

For pages like this IP-direct, Google search:

http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0


How can I change all the links in that page from search?q to search?&q=?

For example, I want to make the link:

http://62.0.54.118/search?q=42&ei=Xf5bUqHLOKeQ0AWV4YG4Cg&start=10&sa=N&filter=0

into:

http://62.0.54.118/search?&q=42&ei=Xf5bUqHLOKeQ0AWV4YG4Cg&start=10&sa=N&filter=0


How can I make Chrome change the links by automatic script or something like that?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
reemar
  • 55
  • 8
  • This question was not asked about, and did not even mention an AJAX scenario. It's "not cricket" to alter a question fundamentally after it's been answered. – Brock Adams Oct 15 '13 at 07:39
  • for those who are looking to change the color of the links (visited or not) on their browser: https://superuser.com/a/1489703/235752 – JinSnow Oct 06 '19 at 16:15

3 Answers3

1

Here is a regular expression that could work for you:

var url = 'http://62.0.54.118/search?q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0'
var regex = /(http:\/\/.*?search\?)(.*)/
console.log( url.replace(regex, '$1&$2') )

// 'http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0'

You will need to loop through all the urls in the page and apply this to each of them.

Do you need help with modifying the HTML in the target page, or can you reach your solution from here?

James Newton
  • 6,623
  • 8
  • 49
  • 113
  • i try to use that answer but without success, i enter at the url place javascript:youranswer and the links remain to be search?q= and not turn to be search?&q= as i want to.. – reemar Oct 14 '13 at 15:46
  • in addition, i want that to work everywhere in the domain '62.0.54.118'. – reemar Oct 14 '13 at 15:47
  • For me, the links on the page your cited do not contain the string `'http://62.0.54.118/search?`. I have created a [fiddle](http://jsfiddle.net/uMgRn/) which adds an `&` character after the `?` character. Does this do what you want? – James Newton Oct 14 '13 at 20:46
  • i make your fiddle to work for me at http://jsfiddle.net/NnHUh/ but how can i apply that scripts to work on the links at http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0 ? – reemar Oct 14 '13 at 22:29
1

To change those links, on a static page (like your example):

  1. Search for applicable links.
  2. Loop through the links and change them. (Typically using regex.)

A complete userscript would like like this:

// ==UserScript==
// @name     _Modify select Google search links
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @include  http://62.0.54.118/*
// ==/UserScript==

var qLinks  = document.querySelectorAll ("a[href*='?q=']");

for (var J = qLinks.length - 1;  J >= 0;  --J) {
    var oldHref = qLinks[J].getAttribute ('href');
    var newHref = oldHref.replace (/\?q=/, "?&q=");

    //console.log (oldHref + "\n" + newHref);
    qLinks[J].setAttribute ('href', newHref);
}


Save the file as GoogleLinkModify.user.js and then drag it to the extensions page to install. (Or install the Tampermonkey extension and install the script via Tampermonkey).


Important:

  1. For AJAX-driven pages, install Tampermonkey and use waitForKeyElements().
  2. Google pages are a PITA to script. Beware that even though you change the href, many Google pages will ignore that and also sneakily redirect links just as you click on them. See or open other questions for more on that.
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • I was using your script and it work great till i reach that url: `http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0#filter=‌​0&q=42&start=90` and every url that contain `#` in the site url, the script jammed.. *how can i make your script to work in place with `#`? *how can i make use in waitForKeyElements() if nessecery? – – reemar Oct 15 '13 at 06:14
  • i had seen that http://stackoverflow.com/questions/10888326/executing-javascript-script-after-ajax-loaded-a-page-doesnt-work but didnt really realize how can i make use in that. – reemar Oct 15 '13 at 06:38
  • What do you mean by "the script jammed", ***exactly***? – Brock Adams Oct 15 '13 at 06:42
  • the script work great, but when i press next, i get to that page `http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0#filter=0&q=42&start=10` )i believe its an ajax driven page(, and the script not apply.. – reemar Oct 15 '13 at 06:45
  • That is, indeed, an ajax-driven page problem, over and above this question, as asked. Mark this question answered and ask a new question for the AJAX problem if you can't figure out how to apply `waitForKeyElements()`. Remember that you need Tampermonkey for that (on Chrome). You should be using Tampermonkey anyway -- for the features and the ease. – Brock Adams Oct 15 '13 at 07:35
  • /i dont know how to mark that question as answered, /i already use Tampermonkey.. when i will open the new question, i will share a link of it to here.. – reemar Oct 15 '13 at 07:51
  • ok, i will mark that as answer, in addition, here is my new question: http://stackoverflow.com/questions/19376094/how-to-change-all-links-in-a-ajaxpage – reemar Oct 15 '13 at 08:06
  • please answer my new question i just asked, thanks: http://stackoverflow.com/questions/19376094/how-to-change-all-links-in-a-ajaxpage – reemar Oct 15 '13 at 08:08
0

A succinct one-liner that can be bookmarked as a scriplet:

javascript:for(i in dl=document.links)dl[i].href=dl[i].href.replace(/(http:.*search[?])(q=.*)/,'$1&$2')

ie. a simple way to observe traversed SO links, whether related or linked or raw, irregardless:

javascript:for(i in dl=document.links)dl[i].href=dl[i].href.replace(/[?].q=1/,"")
ekim
  • 1
  • 1