Dive Into Greasemonkey

Teaching an old web new tricks

3.4. Evaluating expressions with Javascript Shell

Javascript Shell is a bookmarklet that allows you to evaluate arbitrary Javascript expressions in the context of the current page.

Procedure: Install Javascript Shell

  1. Visit Jesse's Web Development Bookmarklets.

  2. Drag the Shell bookmarklet to your links toolbar.

  3. Visit a page (for example, Dive Into Greasemonkey home page), and click the Shell bookmarklet in your links toolbar. The Javascript Shell window will open in the background.

Javascript Shell offers you the same power as DOM Inspector, but in a free-form environment. Think of it as a command line for the DOM. Let's play.

Example: Introduction to Javascript Shell

document.title
Dive Into Greasemonkey
document.title = 'Hello World'
Hello World
var paragraphs = document.getElementsByTagName('p')
paragraphs
[object HTMLCollection]
paragraphs.length
5
paragraphs[0]
[object HTMLParagraphElement]
paragraphs[0].innerHTML
Teaching an old web new tricks
paragraphs[0].innerHTML = 'Live editing, baby!'
Live editing, baby!

Your changes should be reflected in the original page as soon as you hit ENTER.

The only other thing I want to mention about Javascript Shell is the props function.

Example: Get properties of an element

var link = document.getElementsByTagName('a')[0]
props(link)
Methods of prototype: blur, focus
Fields of prototype: id, title, lang, dir, className, accessKey,
charset, coords, href, hreflang, name, rel, rev, shape, tabIndex,
target, type, protocol, host, hostname, pathname, search, port,
hash, text, offsetTop, offsetLeft, offsetWidth, offsetHeight,
offsetParent, innerHTML, scrollTop, scrollLeft, scrollHeight,
scrollWidth, clientHeight, clientWidth, style
Methods of prototype of prototype of prototype: insertBefore,
replaceChild, removeChild, appendChild, hasChildNodes, cloneNode,
normalize, isSupported, hasAttributes, getAttribute, setAttribute,
removeAttribute, getAttributeNode, setAttributeNode,
removeAttributeNode, getElementsByTagName, getAttributeNS,
setAttributeNS, removeAttributeNS, getAttributeNodeNS,
setAttributeNodeNS, getElementsByTagNameNS, hasAttribute,
hasAttributeNS, addEventListener, removeEventListener, dispatchEvent,
compareDocumentPosition, isSameNode, lookupPrefix, isDefaultNamespace,
lookupNamespaceURI, isEqualNode, getFeature, setUserData, getUserData
Fields of prototype of prototype of prototype: tagName, nodeName,
nodeValue, nodeType, parentNode, childNodes, firstChild, lastChild,
previousSibling, nextSibling, attributes, ownerDocument, namespaceURI,
prefix, localName, ELEMENT_NODE, ATTRIBUTE_NODE, TEXT_NODE,
CDATA_SECTION_NODE, ENTITY_REFERENCE_NODE, ENTITY_NODE,
PROCESSING_INSTRUCTION_NODE, COMMENT_NODE, DOCUMENT_NODE,
DOCUMENT_TYPE_NODE, DOCUMENT_FRAGMENT_NODE, NOTATION_NODE,
baseURI, textContent, DOCUMENT_POSITION_DISCONNECTED,
DOCUMENT_POSITION_PRECEDING, DOCUMENT_POSITION_FOLLOWING,
DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_CONTAINED_BY,
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
Methods of prototype of prototype of prototype of prototype of prototype: toString

To coin a phrase, “Whoa!” What's all that about? It's a list of all the properties and methods of that <a> element that are available to you in Javascript, grouped by levels in the DOM object hierarchy. Methods and properties that are specific to link elements (like the blur and focus methods, and the href and hreflang properties) are listed first, followed by methods and properties shared by all types of nodes (like the insertBefore method), and so on.

Again, this is the same information that is available in DOM Inspector... but with more typing and experimenting, and less pointing and clicking.

[Warning]

Like DOM Inspector, Javascript Shell does not “follow” you as you browse. If you open Javascript Shell and then navigate somewhere else in the original window, Javascript Shell will get very confused. It's best to go where you want to go, open Javascript Shell, fiddle to your heart's content, then close Javascript Shell before doing anything else.

← Inspecting elements with DOM Inspector
Other debugging tools →