2013-10-16 108 views
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:

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

我如何可以結合使用這些userscripts在一起嗎?


這是一個不好的解決我試着寫不起作用

我在做什麼錯?

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

我個人認爲,這兩個劇本有足夠的不同讓他們分開。 – phuzi

回答

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