Add custom (JavaScript) conditions in the trigger's condition

82 views
Skip to first unread message

tanaka

unread,
Sep 13, 2024, 3:46:48 PMSep 13
to AutoControl
Sometimes, I only need to override a button for a specific action when a certain web element appears on the page. However, this cannot be easily accomplished within AutoControl alone. Whenever I encounter such situations, I am forced to use a Tampermonkey script.
If a custom script condition can be added in the Trigger's condition, allowing the use of a js bool function for condition check, it will greatly facilitate and expand the capabilities and applicability of  AutoControl.

AutoControl support

unread,
Sep 13, 2024, 4:09:17 PMSep 13
to AutoControl
Can you please provide a simple example of a TamperMonkey script that does what you need?

tanaka

unread,
Sep 13, 2024, 4:22:11 PMSep 13
to AutoControl
```
// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2024-02-14
// @description  try to take over the world!
// @author       You
// @match        https://twitter.com/*
// @match        https://x.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var scrollFunction = function(e) {
        e = e || window.event;

        if (e.ctrlKey) {
            return;
        }
        if ( document.querySelector(`[data-testid=Carousel-NavLeft], [aria-label="上一张幻灯片"]`) == null && document.querySelector(`[data-testid=Carousel-NavRight],[aria-label="下一张幻灯片"]`) == null){
            return;
        }

        e.preventDefault && e.preventDefault(); 
        if (e.wheelDelta) {
            if (e.wheelDelta > 0) { 
                onePageUp();
            }
            if (e.wheelDelta < 0) { 
                onePageDown();
            }

        }
    }

    window.addEventListener('mousewheel', scrollFunction, {
        passive: false
    });

    function onePageUp(){
        document.querySelector(`[data-testid=Carousel-NavLeft], [aria-label="上一张幻灯片"]`).click();
        console.log(`up`)
    }

    function onePageDown(){
        document.querySelector(`[data-testid=Carousel-NavRight], [aria-label="下一张幻灯片"]`).click();
        console.log(`down`)
    }
})();
```

For example, this script only overrides the mouse scroll event when there are elements with [data-testid=Carousel-NavLeft] or [data-testid=Carousel-NavRight] on the page. When the specified elements are not present, the original mouse wheel behavior remains

AutoControl support

unread,
Sep 15, 2024, 3:25:16 AMSep 15
to AutoControl
Here are two different ways to achieve that:

Solution 1:
Make the scroll wheel to click the left/right navigation buttons without overriding its native action.
The attached file "Scroll wheel in Twitter carousel 1.acs" has the complete configuration. You can view the file by going to "Options > View file".
image.png


Solution 2:
Inject the script whenever a Twitter page is loaded. This is equivalent to what TamperMonkey does.
The attached file "Scroll wheel in Twitter carousel 2.acs" has the complete configuration.
image.png

Scroll wheel in Twitter carousel 1.acs
Scroll wheel in Twitter carousel 2.acs

tanaka

unread,
Sep 18, 2024, 10:07:33 PMSep 18
to AutoControl
Thank you, this does indeed provide similar functionality to using a Tampermonkey script, but it still requires writing a full userscript (including adding event listeners). What I was hoping for is the ability to maintain the simplicity of Solution 1 (only needing to select keys and input actions in the GUI) while making it more versatile (for example, sometimes I do need to override the input to prevent the page to scroll when the action is triggered).

AutoControl support

unread,
Sep 19, 2024, 8:26:42 AMSep 19
to AutoControl
Javascript expressions are computationally expensive, this is why they are not supported as trigger conditions. For example, if you turn the mouse wheel quickly, AutoControl would have to evaluate a Javascript expression on the target page many times per second, and depending on the result of the JS expression, AutoControl would have to decide whether to override the mouse wheel action or not. In summary, the Chrome browser cannot evaluate a JS expression fast enough for this process to work smoothly.

You can work around this limitation by using a Switch condition.
image.png

In order to turn the Switch on and off you must use the ACtl.switchState() function from the injected script:
image.png
The attached file "Scroll wheel in Twitter carousel 3.acs" contains the full working configuration.


An alternative workaround is to override the wheel-turn and then re-inject it only if a Javascript condition is true, as follows:
image.png
You must use the ACtl.STOP_CHAIN constant in the script.
Unfortunately, this will not work smoothly enough because the script takes some fraction of a second to execute, so you will notice some short delay in the responsiveness of the scroll wheel.
However, you could still use this method on other cases such as keyboard keys or mouse buttons where there's no need for an instantaneous response.
The attached file "Scroll wheel in Twitter carousel 4.acs" contains the full configuration for this method.

Scroll wheel in Twitter carousel 3.acs
Scroll wheel in Twitter carousel 4.acs
Reply all
Reply to author
Forward
0 new messages