gpt4 book ai didi

javascript - 如何为 Greasemonkey 脚本创建切换按钮?

转载 作者:行者123 更新时间:2023-11-28 09:50:34 25 4
gpt4 key购买 nike

因此,我试图弄清楚如何为油脂猴扩展程序制作一个切换按钮,该扩展程序可以运行并导航到各个页面。

这就是我到目前为止所拥有的

var keepgoing = true

if 语句和此处内容的开头

else if keepgoing == true
{ newsearch(); }

点击按钮:

 if keepgoing == true { keepgoing = false }
else if keepgoing == false { keepgoing = true }

所以基本上我需要帮助在网页上放置一个按钮。并且在浏览页面时需要记住 var。

谢谢,雷

最佳答案

如果您想要一个持久按钮(或任何数据),您将需要使用某种存储。如果涉及多个域,则使用GM_setValue() 。如果所有内容都在同一域中,请使用 localStorage .

我自己添加了这种持久按钮,这是一个精简的脚本,显示了我使用的内容。在我看来,它不仅仅是添加一个按钮,它还使按钮 UI 更加用户友好。

注意:

  1. 按钮状态在页面加载和网站之间保持不变。
  2. 该按钮停留在左上角(由 CSS 设置),当鼠标悬停在其上方时,该按钮会淡出至接近不透明的状态。
  3. 我使用对象,因为有时我会向页面添加多个控件。
  4. 您可以see a demo of how the button appears and behaves at jsFiddle 。 (除了演示中的值无法在页面加载过程中持续存在。它们在 GM 脚本中是这样的。)


// ==UserScript==
// @name _Keep going button
// @include http://YOUR_SERVER_1.COM/YOUR_PATH/*
// @include http://YOUR_SERVER_2.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==

//--- Add the button.
$("body").append (
'<div class="gmPersistentButton">'
+ '<button id="gmContinueBtn">Init failed!</button></div>'
);

//--- Define and init the matching control object:
var btnControl = new PersistentButton (
"gmContinueBtn", //-- HTML id
"StopContinueBtn", //-- Storage label
["Stop", "Continue"], //-- Text that the button cycles through
[false, true] //-- Matching values for the button's states
);

//--- Activate the button click-handler.
$("#gmContinueBtn").click ( function () {
var btnValue = this.value;
keepgoing = btnValue
btnControl.SetNextValue ();

if (keepgoing == "true") {
// CALL YOUR FUNCTION HERE.
}
} );

//--- The button will fade when we aren't using it.
var zDisplayPanel = $('div.gmPersistentButton');
zDisplayPanel.hover (
function () { $(this).stop (true, false).fadeTo (50, 1 ); },
function () { $(this).stop (true, false).fadeTo (900, 0.1); }
);
zDisplayPanel.fadeTo (2900, 0.1);


//--- CSS styles to make it work and to improve appearance.
GM_addStyle ( (<><![CDATA[
.gmPersistentButton {
background: orange;
color: black;
position: fixed;
top: 1em;
right: 1em;
z-index: 6666;
padding: 1em;
border: 3px double gray;
border-radius: 1ex;
opacity: 0.8;
}
.gmPersistentButton button {
cursor: pointer;
}
.gmPersistentButton button:hover {
color: red;
}
]]></>).toString () );


//--- Button object
function PersistentButton (htmlID, setValName, textArry, valueArry) {
//--- Initialize the button to last stored value or default.
var buttonValue = valueArry[0];
fetchValue ();
storeValue (); //-- Store, in case it wasn't already.
setButtonTextAndVal ();

//--- DONE with init. Set click and keyboard listeners externally.

//***** Public functions:
this.Reset = function () {
buttonValue = valueArry[0];
storeValue ();
setButtonTextAndVal ();
};

this.SetNextValue = function () {
var numValues = valueArry.length;
var valIndex = 0;

for (var J = numValues - 1; J >= 0; --J) {
if (buttonValue == valueArry[J]) {
valIndex = J;
break;
}
}
valIndex++;
if (valIndex >= numValues)
valIndex = 0;

buttonValue = valueArry[valIndex];

storeValue ();
setButtonTextAndVal ();
};


//***** Private functions:
function fetchValue () {
buttonValue = GM_getValue (setValName, buttonValue);
}

function storeValue () {
GM_setValue (setValName, buttonValue);
}

function setButtonTextAndVal () {
var buttonText = "*ERROR!*";

for (var J = valueArry.length - 1; J >= 0; --J) {
if (buttonValue == valueArry[J]) {
buttonText = textArry[J];
break;
}
}

var theBtn = document.getElementById (htmlID);
if (theBtn) {
theBtn.textContent = buttonText;
theBtn.setAttribute ("value", buttonValue);
}
else
alert ('Missing persistent button with ID: ' + htmlID + '!');
}
}

关于javascript - 如何为 Greasemonkey 脚本创建切换按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11108745/

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