11 ms·
Just to show folks how simple this is, here's the JavaScript (JXA) version of the AppleScript code needed to run JS in the current active tab: /usr/bin/osas
by varenc 4y ago
Just to show folks how simple this is, here's the JavaScript (JXA) version of the AppleScript code needed to run JS in the current active tab:
/usr/bin/osascript -l JavaScript <<EndOfScript
var current_tab = Application('Google Chrome').windows[0].activeTab();
current_tab.execute({ javascript: 'alert(1)' })
EndOfScript
Once you figure it out it's not too tricky. For example, I have shell utils for converting all links on a page to "in current tab links" by removing the `target` attr on <a> tags:
function chromeJS() {
INPUT="$1";
INPUT=${INPUT//\"/\\\"} ## escape quotes
INPUT=${INPUT//\'/\\\'}
/usr/bin/osascript -l JavaScript <<EndOfScript
var current_tab = Application('Google Chrome').windows[0].activeTab();
current_tab.execute({ javascript: '$INPUT' })
EndOfScript
}
...
$ chromeJS "document.querySelectorAll('a').forEach(a => a.removeAttribute('target')); alert('Made all links in-tab links!')"