【发布时间】: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