Skip to content

tabopen and run js code

Gold Holk edited this page Aug 24, 2022 · 6 revisions

Some websites does allow searching with querystring, so user have to open it and manually input the keyword in textbox. The following scripts can automatically the input process with javascript:

jsurls with js after next open

This command should be called from jsurls, so you can use it with open or tabopen command.

Usage

jsurls can execute js code and open a url, and the command wait_open_and_js will wait the next open website and execute the js code in it. All arguments are executed with the js command in the next opened website. Combine these two features, we can execute the code in the website opened with the open or the tabopen command.

set jsurls.let_me_google_that_for_you keyword => { tri.controller.acceptExCmd(`js_after_next_open const input = document.getElementsByName("q")[0]; input.value = ${JSON.stringify(keyword)}; input.form.submit()`); return 'https://www.google.com/'}
tabopen let_me_google_that_for_you firefox tridactyl add-on

Install

command js_after_next_open jsb -d︿ const code=JS_ARGS.join(' '); const open=browser.webNavigation.onCompleted;const promise=new Promise(ok=>{const nextOpen=e=>{open.removeListener(nextOpen);ok()};open.addListener(nextOpen)});void promise.then(x=>{js(code)}); ︿

tabopen and js

This command will open a url in the new tab, and execute js inside, so one can open a website and do some action with a key or a command. Any improvement is welcome.

Usage

First argument is the url, second argument is the javascript in urlencoding. Following arguments will be passed as arguments.

If the first argument is -s or --switch, it will switch to a existing tab with the same url if exist instead open a new tab.

# tabopen_and_js url urlencode_javascript
tabopen_and_js https://github.com/ alert('hello%20world!')
tabopen_and_js https://github.com/ alert(arguments[0]+arguments[1]) hello wrold
tabopen_and_js -s https://github.com/ alert(arguments[0]+arguments[1]) hello wrold

The await and tri.* API do work, and the whole code is wrapped in a async function. The arguments are always strings.

Complex case

Open a online QRCode generator and generate the QRCode with the clipboard content.

tabopen_and_js -s https://www.nayuki.io/page/qr-code-generator-library const%20e=document.getElementById("text-input");e.value=await%20tri.excmds.getclip('clipboard'),e.dispatchEvent(new%20Event("input")),window.scrollTo(0,e.getBoundingClientRect().top) 

Installation

Tridactyl command:

command tabopen_and_js js -d@ void function(){let switchTo=false;if("-s"==JS_ARGS[1]){JS_ARGS.shift();switchTo=true}void async function(url,js,...args){let tab;if(switchTo){try{tab=await taball(url)}catch(notFound){tab=null}do{await sleep(100);tab=(await tri.browserBg.tabs.query({url:url}))[0]}while(tab&&(tab.discarded||"loading"==tab.status))}if(!tab)tab=await tri.excmds.tabopen(url);const code=`void async function () { ${js} }(...${JSON.stringify(args)})`;await tri.browserBg.tabs.executeScript({code:code})}(JS_ARGS[1],decodeURIComponent(JS_ARGS[2]),...JS_ARGS.slice(3))}(); @

Pretty JavaScript:

void function() {
    let switchTo = false;
    if ("-s" == JS_ARGS[1]) {
        JS_ARGS.shift();
        switchTo = true;
    }
    void async function(url, js, ...args) {
        let tab;
        if (switchTo) {
            try {
                tab = await taball(url);
            } catch (notFound) {
                tab = null;
            }
            do {
                await sleep(100);
                tab = (await tri.browserBg.tabs.query({
                    url: url
                }))[0];
            } while (tab && (tab.discarded || "loading" == tab.status));
        }
        if (!tab) tab = await tri.excmds.tabopen(url);
        const code = `void async function () { ${js} }(...${JSON.stringify(args)})`;
        await tri.browserBg.tabs.executeScript({
            code: code
        });
    }(JS_ARGS[1], decodeURIComponent(JS_ARGS[2]), ...JS_ARGS.slice(3));
}();

Limitation

The javascript context will get lost if the page unload (E.g: submit a form).