-1

I have a userscript that modifies the href of all applicable links on an an IP-direct, Google search page:

// ==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);
}


It works fine on the first page, but when I use the pagination links, it stops working -- because the new pages are loaded by AJAX.

@Brock Adams told me to use waitForKeyElements() but I couldn't figure out how to do it.

I have seen a few topics, like stackoverflow.com/questions/10888326/executing-javascript-script-after-ajax-loaded-a-page-doesnt-work, but I can't figure out how to use them.

How can I use that script to change links on an AJAX page like:

http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0#filter=0&q=42&start=10
Community
  • 1
  • 1
reemar
  • 55
  • 8
  • `search?g=` and `search?&g=` does not make a difference, both send a parameter named `g`… – feeela Oct 15 '13 at 08:09
  • first, the parameter is q, second, its metter to me because, if i didnt add the `&`, the search results redierct to other search engine because of my isp policy.., and if i add the `&` before the `q`, its stay in the original google search that i want.. – reemar Oct 15 '13 at 08:12
  • possible duplicate of [How to change all links in a page?](http://stackoverflow.com/questions/19362617/how-to-change-all-links-in-a-page) - your "new" question fits well into the scope of the first one, especially since the are so closely together time-wise. If you have something to add to your question, please do it there, instead of opening a new one for a topic that is so similar. – CBroe Oct 15 '13 at 08:17
  • 1
    CBroe, i just do what -Bruck Adams- recommend me to.. `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 55 mins ago` – reemar Oct 15 '13 at 08:32
  • @CBroe, the question was asked and answered for a static page. Only then did the OP realize that he needed an AJAX solution and that is a markedly new set of difficulties, especially on a Google site. The static solution is fine for most pages, and simpler to understand. It's not the same question. – Brock Adams Oct 15 '13 at 08:44

1 Answers1

1

To change static-page code to use waitForKeyElements() you do 3 or 4 easy tasks:

  1. Include jQuery and waitForKeyElements with the script's metadata section.
  2. Choose the appropriate jQuery selector for waitForKeyElements. It's often the same as what you would use for querySelectorAll().
  3. Adapt any loop-driven code for a single node. Leveraging jQuery as appropriate.
  4. In this case, Google overwrites links, when you page, rather than use AJAX to place new ones. So have the callback function return true.

Putting it all together, the complete script based on the question code would be:

// ==UserScript==
// @name     _Modify select Google search links
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @include  http://62.0.54.118/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("a[href*='?q=']", changeLinkQuery);

function changeLinkQuery (jNode) {
    var oldHref = jNode.attr ('href');
    var newHref = oldHref.replace (/\?q=/, "?&q=");

    jNode.attr ('href', newHref);

    return true;
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • See [your privileges page](http://stackoverflow.com/help/privileges). Voting up requires 15 points. – Brock Adams Oct 15 '13 at 08:48
  • i need help in other qustion: http://stackoverflow.com/questions/19403763/how-can-i-combine-two-userscripts-into-one – reemar Oct 16 '13 at 12:45