Using JavaScript DOM Methods

Probably a question for @agiletortoise, but if anyone else knows the answer let me know. I am working on an action that processes some HTML. I’m still learning JavaScript and came across the methods for working with the DOM within the language. These all seem to centre around the document object. In the Drafts scripting environment, is there any way to initialise this document object? Ideally I’d like to set it to the current draft, so I can interact with it using the DOM methods. Is this possible?

I wouldn’t have thought so – the DOM API is an interface to parsed HTML pages in web browsers.

The DOM library is not part of the Javascript language, and it doesn’t provide a model of plain text files.

(For a long time, JavaScript was only embedded in web browsers, so there is understandably still a lot of material out there which speaks of JS and the DOM in one breath, as if they were the same thing – it’s a common assumption).

JavaScript is an embedded language, and the libraries and objects available to it depend on what kind of application it is embedded in.

The quickest way to find out is to evaluate the expression:

Object.keys(this)

Which returns the names of the top level objects available in the global namespace of the JS interpreter.

If you do that in a Chrome or Safari console, you will see all of the DOM objects (Window and Document etc etc)

If you do it in Drafts, 1Writer, Taskpaper, or the OmniJS of Omni Applications, you will see very different (application-specific) lists of available objects.

For a fuller picture, see for example
http://actions.getdrafts.com/a/1F5

or, more helpfully:

https://github.com/agiletortoise/drafts-documentation/wiki

3 Likes

Editorial does, of course, include a browser, and there is a method for evaluating JS snippets in, but not, as far as I recall, a way of passing data to those snippets at run time, other than using a fairly fiddly mechanism of loading quoted JSON from the file system.