【问题标题】:How can I combine two userscripts into one?如何将两个用户脚本合二为一?
【发布时间】:2013-10-16 12:43:38
【问题描述】:

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

第一个:

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

【问题讨论】:

  • 我个人认为这两个脚本的不同足以将它们分开。

标签: javascript ajax google-chrome userscripts


【解决方案1】:

这两个脚本都有一个名为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;
}

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多