Access link in browser through right click, Applescript and/or Javascript? - javascript

I want to right click a regular link in (primary) Chrome, Safari (secondary) or Firefox/Tor Browser (tertiary) and then send it somewhere else using Applescript. How do I do this?
I could imagine that I first use Javascript to get the actual link and then return it as text to an Applescript for further processing. Does it make sense? If so, I would appreciate some hints about how to get going.

You can do this by way of an Automator service, which will add a contextual (right-click) menu item when you click on a link in Safari (or Chrome).
Read the Mac Automation Scripting Guide: Making a Systemwide Service, which has a step-by-step guide on how to create an Automator service.
It will look something like this during construction:
This service, as you can see, is active only in Safari when there is a block of text selected. Unfortunately, you can't limit it to just hyperlinks, but it does include them.
If the service is available (i.e. you are in Safari and have selected some text), the menu item corresponding to the service appears in the right-click submenu called "Services", and it'll be named according to what you saved the workflow as in Automator. In my case, the service is named "Process URL".
When clicked, it executes the AppleScript you can see in the workflow (it does this in the background, invisibly).
This is where your suggestion of using JavaScript was a good one to employ: the AppleScript executes a bit of JavaScript in Safari (provided you have the right permissions enabled for it to do so, ticked in Safari's "Develop" menu).
This JavaScript returns one of two results:
▸ If the text selected is plain text with no hyperlink underneath it, it returns an empty string;
▸ If the text selected is hyperlink text, it returns the hyperlinked URL.
It stores this in the variable href, which you can then do whatever you want with.
The good thing about hyperlinks is that you don't have to click and drag to select the text (although you can if you wish, and only part of the text needs to be selected). Instead, you can just right-click on a link, and it automatically selects it for you, making it ideal to activate the service is this manner.

Related

How do I click a dialog button in MS Word on MacOS via Apple's Javascript for Automation?

I'm looking to click dialog boxes that appear in MS Word by using Apple's JXA automation tooling.
I'd like to click on the following dialog boxes:
Word found unreadable content in Do you want to recover the contents of this document? If you trust the source, click Yes
Word detected and repaired the following errors. To view each repair...
I'd like to click Yes on the first one, and close on the second one.
For context, I'm looking to extend the docx2pdf library in order to automatically skip these dialogs when converting a file to PDF. Right now, there are some manual clicks required due to these dialogs that I would like to fully automate.
What is the JXA code that I would need to automate this task?

How to select text on chrome DOM with cursor and access it on chrome extension

This question is a bit confusing and I am sorry I do not have any code to provide. I am just interested in knowing if this is generally possible or if it is a limitation of chrome extensions.
Currently, I am thinking of building a chrome extension which should have a "Manual Select button". When this button is clicked, users should be able to highlight (or select) text on the Chrome DOM using their cursor. The text they highlight or select should then be saved and displayed in the Chrome extension (I am guessing I will put that in local storage or something). The problem I am facing with this idea, is that I notice Chrome extensions close when you click away meaning the text you select will not be saved in the Chrome extension. Is there any way to force the extension to stay open even if you click away? Or, is there a way to have the extension still run the button's function logic while it is closed?
If this is possible and can provide some sudo code of how this would work it is greatly appreciated!

Javascript bring a tab to the front

I am writing a Javascript which runs in the browser using Tampermonkey.
The logic in the script is as follows.
1. Do some checks.
2. If a certain condition is met, then trigger an alert box in the browser.
Currently the alert box is shown in the browser tab, but amidst multiple tabs, the user does not notice the alert box till they navigate to the tab.
I am trying to figure out one or both of the following.
1. Bring the tab to the front i.e. focus on that tab.
2. Do something more aggressive than the alert box to catch user's attention. I thought of things such as playing sounds etc but it requires on user's speakers being on etc.
Any ideas on how to make this work would be greatly appreciated.
Looks like this cannot be done and the easiest option would be to write some data to the browser's local storage and then open a new window, fetch the data and display it.

How to change user selected text in JavaScript

I would like to change the text that will be pasted without changing the selection's appearance on the screen, similar to what the Linux xsel command can do.
Ideally this would work on all platforms and browsers, but at the very least I want it to work on Chrome running on Linux.
My usecase is that I have a button that selects a block of shell commands from the web page (this is done by some library magic that I have no easy access to) and I want to add a terminating newline "behind the scenes" so that when the user middle-clicks in the console, he gets the text that appears highlighted on the screen plus a newline.
How to do it?

Hit a button within the browser that would un-focus the browser then focus a different application?

Is that possible using AppleScript on macOS or VBScript on Windows? Desktop only.
Thanks!
Yes, it's possible. It would involve an applescript that would be called by clicking a hyperlink in the web page. You can find additional information here
Basically, your Applescript would be responsible for changing the front application. The Applescript would be triggered by the link in the website.
Note :
The user has to "approve" the running of the script initially, but after that, it's capable of running once the link is clicked without additional user input.

Categories