如何更改ajax驱动页面中的所有链接? [英] How to change all links in an ajax driven page?

查看:142
本文介绍了如何更改ajax驱动页面中的所有链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本修改了IP直接Google搜索页面上所有适用链接的href:

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



它在第一页上正常工作,但当我使用分页链接时,它停止工作 - 因为新页面由AJAX加载。


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告诉我使用 waitForKeyElements ()但我无法弄清楚如何做到这一点。

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

我看过几个主题,比如 stackoverflow.com/questions/10888326/executing-javascript-script-after-ajax-我不知道如何使用它们。

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.

如何使用该脚本来更改链接在一个AJAX页面上:

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


推荐答案

要将静态页面代码更改为使用 waitForKeyElements(),请执行3或者4个简单的任务:

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


  1. 包含jQuery和waitForKeyElements以及脚本的元数据部分。

  2. 选择适用于waitForKeyElements的适当的jQuery选择器。它通常与用于 querySelectorAll()的相同。

  3. 适用于单个节点的任何循环驱动代码。适当利用jQuery。

  4. 在这种情况下,Google在页面上覆盖链接,而不是使用AJAX放置新链接。因此,回调函数返回 true

  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.

strong>基于问题代码的完整脚本应该是:

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;
}

这篇关于如何更改ajax驱动页面中的所有链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆