Extract Chrome URL and Copy to Clipboard

Variations of this questions have been asked and answered more than once but I cannot find a viable solution with my limited coding abilities that do not involve Click at Found Image.

I'd like to copy a Chrome URL behind an icon to the clipboard and paste into Firefox. I'm using Firefox to avoid cookie tracking in Chrome since that would contaminate Adword results and I do not need thousands of retargeting ads in my work profile.

In this example I'm trying to copy http://www.websiteexample.com into the clipboard and everything is easy enough.

Screenshot 2024-03-16 at 6.32.31 PM
Screenshot 2024-03-16 at 6.33.10 PM

Ctrl-click for a contextual menu, followed by choosing Copy Link from that menu ?

In other words are you asking for a GUI operation,
or for a mapping from the unique class name of that div (zp_I1ps2, in this case) to the first enclosed URL ?

What's the work-flow / use case that you have in mind ?

Yes, I'm trying to copy the link in the most efficient way without using Found Image. It's possible with found image but the macro gets messy.

When a customer calls a chrome pop-up with their account info appears on my screen. I'd like to open their website, Linked-in, and or Facebook in Firefox (to avoid cookies in Chrome) with one macro. Those pages would then be moved to a second monitor before answering the phone.

The goal is to have an intelligent conversation about their business even if I've never spoken with them. I can do everything but capture the link behind the icon.

Hard to say without seeing the pop-up page, but it looks as if the class names may be different in each case, and so not predictable enough to be useful in tracking the icon down.

Another possibility would be to open all links on the page, harvesting them with an XPath query like //a, but perhaps that will open some redundant links too ?

in Firefox

Firefox provides no osascript interface, and is largely beyond the scope of Keyboard Maestro automation.

To harvest a list of all the URLs on the front Chrome page, one approach is to use code like that below, and you may be able to fine-tune the XPath query to narrow the number of its targets a bit:

List of all URLs in front Chrome page.kmmacros (4.4 KB)

Expand disclosure triangle to view browser JS source
return (() => {
    "use strict";

    const main = () =>
        Array.from(
            xPathMatches(kmvar.local_XPath)
        )
        .map(x => x.href)
        .join("\n");

    // xPathMatches :: XPath String -> Gen [Node]
    const xPathMatches = xpath =>
        // A list of any matching nodes in the document.
        // (Can be made strict by wrapping in Array.from)
        unfoldr(x => {
            const v = x.iterateNext();

            return Boolean(v)
                ? Just([v, x])
                : Nothing();
        })(
            document.evaluate(
                xpath,
                document,
                null,
                XPathResult.ORDERED_NODE_ITERATOR_TYPE
            )
        );

    // Just :: a -> Maybe a
    const Just = x => ({
        type: "Maybe",
        Just: x
    });

    // Nothing :: Maybe a
    const Nothing = () => ({
        type: "Maybe",
        Nothing: true
    });

    // unfoldr :: (b -> Maybe (a, b)) -> b -> Gen [a]
    const unfoldr = f =>
    // A lazy (generator) list unfolded from a seed value
    // by repeated application of f to a value until no
    // residue remains. Dual to fold/reduce.
    // f returns either Nothing or Just (value, residue).
    // For a strict output list,
    // wrap with `list` or Array.from
        x => (
            function* () {
                let maybePair = f(x);

                while (!maybePair.Nothing) {
                    const valueResidue = maybePair.Just;

                    yield valueResidue[0];
                    maybePair = f(valueResidue[1]);
                }
            }()
        );

    return main();
})();

Firefox is more or less irreverent, the issue is getting the URL to the clipboard. Your solution is so far above my pay grade- I just don't understand scripting yet. Thank you for trying to help. This is my workaround and it seems to be working so no need to complicate it.

The last step is a macro that moves Firefox the another monitor.

1 Like

Good to see a fellow functional programmer :slight_smile:

1 Like