【问题标题】:How to change all links in an ajax driven page?如何更改 ajax 驱动页面中的所有链接?
【发布时间】:2013-10-22 22:54:47
【问题描述】:

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

// ==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 加载的。

@Brock Adams 告诉我使用 waitForKeyElements(),但我不知道该怎么做。

我看过一些主题,例如stackoverflow.com/questions/10888326/executing-javascript-script-after-ajax-loaded-a-page-doesnt-work,但我不知道如何使用它们。

如何使用该脚本更改 AJAX 页面上的链接,例如:

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

【问题讨论】:

  • search?g=search?&g= 没有区别,都发送一个名为g的参数...
  • 第一,参数是q,第二,它对我来说很重要,因为如果我没有添加&,由于我的isp政策,搜索结果会重定向到其他搜索引擎..,如果我在q 之前添加&,它留在我想要的原始谷歌搜索中..
  • How to change all links in a page? 的可能重复项 - 您的“新”问题非常适合第一个问题的范围,特别是因为它们在时间上是如此紧密地结合在一起。如果您有什么要添加到您的问题,请在那里做,而不是为一个如此相似的主题打开一个新的。
  • CBroe,我只是做 -Bruck Adams- 推荐我做的事。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
  • @CBroe,针对静态页面提出并回答了该问题。直到那时,OP 才意识到他需要一个 AJAX 解决方案,这是一组明显的新困难,尤其是在 Google 网站上。静态解决方案适用于大多数页面,并且更易于理解。这不是同一个问题。

标签: javascript ajax google-chrome userscripts tampermonkey


【解决方案1】:

要将静态页面代码更改为使用 waitForKeyElements(),您需要完成 3 或 4 个简单任务:

  1. 在脚本的元数据部分中包含 jQuery 和 waitForKeyElements。
  2. 为waitForKeyElements 选择适当的jQuery 选择器。它通常与您用于 querySelectorAll() 的相同。
  3. 为单个节点调整任何循环驱动的代码。酌情利用 jQuery。
  4. 在这种情况下,当您翻页时,Google 会覆盖链接,而不是使用 AJAX 来放置新链接。所以让回调函数返回true

综上所述,基于问题代码的完整脚本将是:

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

【讨论】:

猜你喜欢
  • 2013-10-22
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 2014-01-25
  • 2017-12-19
  • 1970-01-01
相关资源
最近更新 更多