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