Skip to content Skip to sidebar Skip to footer

How To Change All Links In An Ajax Driven Page?

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 // @inc

Solution 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);

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

    jNode.attr ('href', newHref);

    returntrue;
}

Post a Comment for "How To Change All Links In An Ajax Driven Page?"