Toggling click handler using waitForKeyElements

2019-09-10 11:10发布

I recently discovered, waitForKeyElements. An amazing utility from BrockA.

I'm using the userscript below in Tampermonkey to automatically accept chats from SalesForce's LiveAgent.

Works great, my question is if there's a way to add a button or a link to toggle between it, without having to flip the disable & enable switch on Tampermonkey, and then refreshing the page?

Any help is greatly appreciated since I've spent a few days looking for an answer, and very little to show for. Thanks in advance!!

// ==UserScript==
// @name         AutoAccept Chats
// @version      0.1
// @author       Me
// @include      https://*.ladesk.com/agent/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require      https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant        GM_addStyle
// ==/UserScript==
'use strict';

waitForKeyElements (
    "div.TicketNotificationWindowIn",
    AnswerChat
);

function AnswerChat (jNode) {
    document.getElementsByClassName("ImLeButtonImLeButtonMainOut TicketNotificationAcceptButton")[0].click();
}

This is what I had which didn't work, but might help explain my question.

var turnsOff = 1;
function doToggle() {
if (turnsOff == 1) {
turnsOff = 2;
}else{
turnsOff = 1;
}
ToggleState();
}

function ToggleState () {
if (turnsOff == 1) {
waitForKeyElements (
"div.TicketNotificationWindowIn",
AnswerChat
);
      }else{
          console.log('not answered');
      }
}

0条回答
登录 后发表回答