gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:49:21 25 4
gpt4 key购买 nike

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

第一个:

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

最佳答案

这两个脚本都有一个名为 changeLinkQuery 的函数,但是changeLinkQuery每个都不一样!加上 changeLinkQuery 之一functions 被错误命名,因为它没有改变任何链接的查询部分。

此外,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;
}

关于javascript - 如何将两个用户脚本合并为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19403763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com