1

我写了两个非常相似的脚本,我希望它们都在一个脚本下一起工作,我该怎么做?

首先:

// ==UserScript==
// @name     Normal Google
// @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;
}


第二个用户脚本:

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

如何将这些用户脚本组合在一起?


这是一个糟糕的解决方案,我尝试编写它不起作用

我究竟做错了什么?

// ==UserScript==
// @name     Google
// @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=']","input[name*='q']", changeLinkQuery);

function changeLinkQuery (j1,j2) {
    var oldHref = j1.attr ('href');
    var newHref = oldHref.replace (/\?q=/, "?&q=");
    var oldName = j2.attr ('name');
    var newName = oldName.replace (/q/, "&q");

    j1.attr ('href', newHref);
    j2.attr ('name', newName);

    return true;
}
4

1 回答 1

2

这两个脚本都有一个名为 的函数changeLinkQuery,但changeLinkQuery每个脚本都不相同!加上其中一个changeLinkQuery函数名称错误,因为它没有更改任何链接的查询部分。

此外,waitForKeyElements它不会像那样采用多个选择器字符串,但一个 jQuery 选择器可以有多个部分。

所以这很糟糕:"a[href*='?q=']","input[name*='q']"

但这会起作用:"a[href*='?q='],input[name*='q']"但不是适合您情况的最佳方法。

解决方案是重命名其中一个changeLinkQuery函数,如下所示(也包含您之前问题的修复):

// ==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 ("a[href*='?q=']",  changeLinkQuery);
waitForKeyElements ("input[name='q']", changeInputName);

function changeLinkQuery (jNode) {
    var oldHref = jNode.attr ('href');
    var newHref = oldHref.replace (/\?q=/, "?&q=");

    jNode.attr ('href', newHref);
    return true;
}

function changeInputName (jNode) {
    var oldName = jNode.attr ('name');
    var newName = oldName.replace (/q/, "&q");

    jNode.attr ('name', newName);
    return true;
}
于 2013-10-17T01:50:10.423 回答