Safari—duplicate tab macro

Hello!

I assigned the following script to a Bookmark in Safari, and it flawlessly creates a duplicate of the adjacent tab's linked web page.

javascript:var%20docURL%20=%20document.URL;var%20open_link%20=%20window.open('','_blank');open_link.location=docURL;

However, after I assigned this script to a KM Execute JavaScript in Safari action in a new Safari macro, nothing happened when I activated the macro.

What do I need to do differently so that the macro will function properly?

Many thanks in advance!

The bookmark in Safari, like any web browser bookmark, contains a URI (URL); a JavaScript "bookmarklet" is no different: instead of an http: scheme that tells the browser to use a specific protocol to fetch a remote resource, a bookmarklet uses a javascript: scheme to tell the browser to parse the rest of the string as JavaScript code.

Note that, like other URLs, the JavaScript URL is percent-encoded, hence the many "%20" appearances that indicate encoded ⟨space⟩ characters.

The KM Execute JavaScript in Safari action doesn't take a URL as its input; it takes plain, unencoded JavaScript code. Therefore, to get your code to work, you need to do two things:

  1. Remove the URI scheme, i.e. javascript:
  2. Percent decode the URL string back into plain, unencoded text

Number ② in this situation is actually pretty straightforward, as the only encoded character is the ⟨space⟩ character. So everywhere you see "%20", just replace it with a ⟨space⟩.

The final code looks like this:

var docURL = document.URL;var open_link = window.open('','_blank');open_link.location=docURL;

which can (optionally) be reformatted to be a bit more readable into this:

var docURL = document.URL;
var open_link = window.open('','_blank');
open_link.location=docURL;

and then, finally—and, again, optionally—be simplified to this:

window.open(document.URL);

Hi!

Many thanks for your posting.

Nothing happened after entering each of the three versions (separately) of the corrected script.

I have another KM macro for Safari ("Really quit?") which does execute properly.

What do I need to do differently so that the corrected script also will execute properly?

① Create this action:

② Make sure this menu item is ticked:

image

Alternatively, you don't need to use JavaScript as all if you don't want to. You can just use this action:

1 Like

Perfect, many thanks!