add JavaScript

Cameron Kaiser 2019-04-01 01:31:01 +00:00
parent 0513023989
commit f83bc9de56

@ -1,4 +1,4 @@
**AppleScript support is experimental and may change in future versions of TenFourFox.**
**AppleScript support is experimental and may change in future versions of TenFourFox. This document refers to the AppleScript dictionary in the latest version of TenFourFox.**
TenFourFox FPR13 and later can be controlled via AppleScript. Using AppleScript, you can open and close browser windows and tabs, read selected text content for further handling, visit URLs, and access plaintext and HTML versions of their contents. If you enable GUI scripting, you can even send click and keyboard events to webpages and many TenFourFox internal widgets and interact with them programmatically. However, because of Firefox/Gecko's cross-platform nature, TenFourFox's AppleScript support uses slightly different idioms. This page assumes you have basic knowledge of AppleScript commands and concepts.
@ -96,6 +96,36 @@ end tell
This could be the basis of things you put in the Scripts menu for interactive operations on web page text. If images are selected, then their "alt texts" are used instead.
## AppleScript-JavaScript bridging
TenFourFox's AppleScript support contains an AppleScript-to-JavaScript "bridge." Tabs can be told to execute embedded JavaScript in your AppleScript (`run JavaScript`), and the embedded scripts can return values back to the main AppleScript. This allows you to manipulate the page in the tab and query or change the DOM. Consider the following script:
```
tell application "TheApp"
tell front browser window
set URL of current tab to "https://www.google.com/"
repeat while (current tab is busy)
delay 1
end repeat
tell current tab
run JavaScript "let f = document.getElementById('tsf');f.q.value='tenfourfox';f.submit();"
end tell
repeat while (current tab is busy)
delay 1
end repeat
tell current tab
run JavaScript "return document.getElementsByTagName('h3')[0].innerText + ' ' + document.getElementsByTagName('cite')[0].innerText"
end tell
end tell
end tell
```
This script goes to Google and searches for `tenfourfox`, then returns the name and location of the first result. You should check `busy` if you make any changes to the state of the page before continuing.
Embedded JavaScript runs at chrome level, and within a `Function` context. There is no global object, so you must explicitly refer to `document` and `window` (e.g., it's not `alert()`, it's `window.alert()`). However, any supported `document` or `window` function can be run by the bridge. Embedded JavaScript may return at most one value.
While you can embed subfunctions within your code, you should not attempt to make the state of your script persist between calls to `run JavaScript`, nor attempt to make any objects you create permanent in the DOM; this is not supported, and probably won't work reliably.
## GUI scripting
When GUI scripting is enabled (make sure `Enable access for assistive devices` is checked in `System Preferences` and/or check `Enable GUI Scripting` in `AppleScript Utility.app`), you can manipulate some TenFourFox widgets and send clicks and keyboard events to the application. There is one irregularity in that the `System Events` target is `TenFourFox` always, not the filename of the actual browser (don't change that in these examples, just TheApp as before).