Giter Site home page Giter Site logo

Comments (3)

nikitavoloboev avatar nikitavoloboev commented on May 7, 2024

I have macros that will open 1st/2nd/3rd link when I am on Google for me.

For example I have bound this macro to ctrl+v:

2019-04-02 at 19 28

And it will instantly open first result on Google for me.

I also use sVim and have bindings configured. Pressing d will open link hints and I can choose results by typing letters:

2019-04-02 at 19 30

from ama.

nikitavoloboev avatar nikitavoloboev commented on May 7, 2024

The macro calls this JXA:

(() => {
    'use strict';

    const main = () => {
        const
            linkIndex = 0,

            strXPath = "//*[@class='r']/a",
            saf = Application("Safari"),
            ws = saf.windows;
        return bindLR(
            0 < ws.length ? (
                Right(ws.at(0))
            ) : Left('No window open in Safari'),
            w => {
                const
                    xs = pageXPathHarvest(
                        saf, w, strXPath
                    );
                return 0 < xs.length ? (
                    // Safari effect

                    //tabsOpened(saf, w, xs),
                    nthLinkOpened(saf, xs, linkIndex),

                    // Keyboard Maestro value
                    Right(
                        xs.reduce(
                            (a, link) =>
                            `${a}[${link[0]}](${link[1]})\n`,
                            ''
                        )
                    )
                ) : Left(
                    'Perhaps not a Google search page ?\n' +
                    '(No links matching "' + strXPath + '")'
                );
            }
        );
    };

    // SAFARI ---------------------------------------------

    // Harvest elements from Safari by XPath pattern
    const pageXPathHarvest = (browser, oWin, strXPath) =>
        browser.doJavaScript(
            `(${xpathHarvest})("${strXPath}")`, { in
                : oWin.currentTab
            }
        );

    // tabsOpened :: Application -> Window -> (String, String) -> IO()
    const tabsOpened = (safari, oWin, links) => {
        const winTabs = oWin.tabs;
        links.map(link => winTabs.push(safari.Tab({
            url: link[1]
        })))
    };

    // nthLinkOpened :: Application -> (String, String) -> Int -> IO()
    const nthLinkOpened = (safari, links, i) =>
        (ds =>
            (
                ds.length < 1 && ds.push(safari.Document()),
                ds.at(0)
            )
            .url = links[i][1]
        )(safari.documents);

    // Harvesting function to run in the browser context
    const xpathHarvest = strPath => {
        const
            r = document.evaluate(strPath, document, null, 0, null),
            xs = [];
        let oNode;
        while (oNode = r.iterateNext()) {
            xs.push([oNode.text, oNode.href]);
        }
        return xs;
    };

    // GENERIC FUNCTIONS ----------------------------------

    // https://github.com/RobTrew/prelude-jxa

    // Left :: a -> Either a b
    const Left = x => ({
        type: 'Either',
        Left: x
    });

    // Right :: b -> Either a b
    const Right = x => ({
        type: 'Either',
        Right: x
    });

    // bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
    const bindLR = (m, mf) =>
        m.Right !== undefined ? (
            mf(m.Right)
        ) : m;

    // MAIN
    return main();
})();

I share all macros I use on Gumorad. 🙂

from ama.

nojusmickus avatar nojusmickus commented on May 7, 2024

Thank you :)

from ama.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.