我怎样才能将两个userscripts成一个? [英] How can I combine two userscripts into one?

查看:156
本文介绍了我怎样才能将两个userscripts成一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了两个剧本,真的是相似的,我希望他们两个在一个脚本一起工作,我该怎么办呢?

第一:

  // == UserScript ==
// @name普通谷歌
// @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 ==
/ *  - 的@grant指令需要解决设计变更
    通用汽车1.0推出。它恢复了沙箱。
* /
waitForKeyElements(一个[HREF * ='Q ='],changeLinkQuery);

功能changeLinkQuery(jNode){
    变种oldHref = jNode.attr('的href');
    VAR newHref = oldHref.replace(?/ \ Q = /,&放大器; Q =);

    jNode.attr(HREF,newHref);

    返回true;
}
 


第二userscript:

  // == UserScript ==
// @name普通谷歌输入
// @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 ==
/ *  - 的@grant指令需要解决设计变更
    通用汽车1.0推出。它恢复了沙箱。
* /
waitForKeyElements(输入[姓名* ='Q'],changeLinkQuery);

功能changeLinkQuery(jNode){
    VAR使用oldName = jNode.attr('名');
    变种了newName = oldName.replace(/ Q /,与& Q);

    jNode.attr(名称,了newName);

    返回true;
}
 

我怎么能结合这些userscripts在一起吗?


这是一个的解决方案,我试着写的不能正常工作

我是什么做错了吗?

  // == UserScript ==
// @name谷歌
// @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 ==
/ *  - 的@grant指令需要解决设计变更
    通用汽车1.0推出。它恢复了沙箱。
* /
waitForKeyElements(一个[HREF * ='Q ='],输入[姓名* ='Q'],changeLinkQuery);

功能changeLinkQuery(J1,J2){
    变种oldHref = j1.attr('的href');
    VAR newHref = oldHref.replace(?/ \ Q = /,&放大器; Q =);
    VAR使用oldName = j2.attr('名');
    变种了newName = oldName.replace(/ Q /,与& Q);

    j1.attr(HREF,newHref);
    j2.attr(名称,了newName);

    返回true;
}
 

解决方案

这两个脚本都有一个名为函数 changeLinkQuery ,而 changeLinkQuery 不是每个相同的!另外的 changeLinkQuery 的功能之一是名不副实的,因为它没有改变任何链接的查询部分。

此外, waitForKeyElements 不采取多重选择器串像,而是一个jQuery选择可以有多个部分。

所以,这是不好的:一[HREF * ='Q ='],输入[名* ='Q']

不过,这将工作:一[HREF * ='Q ='],输入[名* ='Q'],但不是最好的方法您的具体情况。

解决的办法是要重命名的 changeLinkQuery 的功能之一,像这样(还结合的从previous问题)修复:

  // == UserScript ==
// @name普通谷歌输入
// @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 ==
/ *  - 的@grant指令需要解决设计变更
    通用汽车1.0推出。它恢复了沙箱。
* /
waitForKeyElements(一个[HREF * ='Q ='],changeLinkQuery);
waitForKeyElements(输入[名称='Q'],changeInputName);

功能changeLinkQuery(jNode){
    变种oldHref = jNode.attr('的href');
    VAR newHref = oldHref.replace(?/ \ Q = /,&放大器; Q =);

    jNode.attr(HREF,newHref);
    返回true;
}

功能changeInputName(jNode){
    VAR使用oldName = jNode.attr('名');
    变种了newName = oldName.replace(/ Q /,与& Q);

    jNode.attr(名称,了newName);
    返回true;
}
 

I wrote two scripts that are really similar, and I want them both to work together under one script, how can I do it?

The First:

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


The Second userscript:

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

How can I combine those userscripts together?


This is a bad solution that I try to write that does not work.

What am I doing wrong?

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

解决方案

The two scripts both have a function named changeLinkQuery, but changeLinkQuery is not the same in each! Plus one of the changeLinkQuery functions is misnamed, because it is not changing any link's query part.

Also, waitForKeyElements does not take multiple selector strings like that, but a jQuery selector can have multiple parts.

So this is bad: "a[href*='?q=']","input[name*='q']"

But this will work: "a[href*='?q='],input[name*='q']" but is not the best way for your situation.

The solution is to rename one of the changeLinkQuery functions, like so (also incorporating 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;
}

这篇关于我怎样才能将两个userscripts成一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆