![](/img/trans.png)
[英]how can a userscript get notified about ajax-driven changes on the page?
[英]How to change the name field of an <input> in an AJAX-driven page?
对于此目标页面(对不起,但是SO不允许超链接到62.0.54.118):
http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0
,我想使用用户脚本默认更改<input>
的名称字段。
输入为:
<input class="gsfi" id="lst-ib" name="q" maxlength="2048" value="42"...>
我想将其更改为:
<input class="gsfi" id="lst-ib" name="&q" maxlength="2048" value="42"...>
也就是说,默认情况下,我要在输入的name
字段中将q
更改为&q
。
我尝试编写一个脚本,(不起作用):
// ==UserScript==
// @name Normal Google Input
// @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 ("input[name*='q']", changeLinkQuery);
function changeLinkQuery (jNode) {
var oldName = jNode.attr ('name');
var newName = oldName.replace (/\q/, "&q");
jNode.attr ('name', newName);
return true;
}
另外,该页面是Ajax驱动的页面。
请修复错误的脚本或帮助我编写另一个脚本,谢谢。
更新:我通过将脚本的一行更改为来解决了部分问题:
var newName = oldName.replace (/\q/, "&q");
至
var newName = oldName.replace (/q/, "&q");
然后我的脚本效果更好。 感谢@Gerard Sexton提出的建议。
但是现在出现了一个新错误,其中waitForKeyElements
回调设置为return true;
对于页面的AJAX,它添加&
不停。
它使该字段为name="&&&&&&&&&q"
等。
我该如何解决?
如果输入名称本身只是q
,则只需将waitForKeyElements调用更改为:
waitForKeyElements ("input[name='q']", changeLinkQuery);
这样它会精确地查找q
,而不是字符串中任何位置的aq。
如果这不完全是该<input>
的名称,请在下面评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.