如何将两个用户脚本合并为一个?

时间:2013-10-16 12:43:39

标签: javascript ajax google-chrome userscripts

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

第一个:

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

1 个答案:

答案 0 :(得分:2)

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

此外,waitForKeyElements不会使用多个选择器字符串,但jQuery选择器可以包含多个部分。

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

但这可行:"a[href*='?q='],input[name*='q']"但不适合您的情况。

解决方案是重命名其中一个changeLinkQuery函数,如此(也包含the fix from your previous question):

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