1

对于此目标页面(抱歉,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"...>


也就是我想默认将输入字段中的inq改为&qin 。name

我尝试编写一个脚本,(这不起作用):

// ==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"等。

我该如何解决?

4

1 回答 1

1

如果输入名称q本身只是 ,那么只需将 waitForKeyElements 调用更改为:

waitForKeyElements ("input[name='q']", changeLinkQuery);

这样它就可以在字符串中的任何位置精确 q查找,而不是 aq。


如果那不完全是那个名字,请<input>在下面评论。

于 2013-10-17T01:28:31.740 回答