Firefox:Dive Into Greasemonkey/3.4. Evaluating expressions with Javascript Shell
Mozilla中文Wiki
您的位置:Dive Into Greasemonkey → 调试用户脚本 → 使用 Javascript Shell 获取元素信息
3.4. 使用 Javascript Shell 获取元素信息
Javascript Shell 是一个书签页,它使您可以执行当前网页中的Javascript脚本。
1. 访问Jesse's Web Development Bookmarklets (http://www.squarefree.com/bookmarklets/webdevel.html)
2. 将Shell bookmarklet放到书签工具栏。
3. 访问一个网页(例如, Dive Into Greasemonkey (http://diveintogreasemonkey.org) ), 单击书签工具栏中的Shell。此时,Javascript Shell窗口就会在后台打开。
Javascript Shell与DOM查看器有相同的功能,不过更加自由。不妨认为它就是DOM查看器的命令行模式。下面我们会介绍它。
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!
当您输入完上面的内容后,按回车,您将会看到网页上的变化。
此外,我还想强调关于Javascript Shell的另一个问题,那就是 props 函数。
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
哇喔!上面所显示的这些内容都是什么呢?它们是 <a> 这个元素所具有的所有的属性和方法。 它们按照 DOM 的对象层次显示出来。首先列出的是 <a> 所独有的属性和方法(例如 blur 和 focus 方法, 以及 href 和 hreflang 属性), 接着是所有节点共享的属性和方法 (例如 insertBefore 等等)。
这又和DOM查看器中的信息相同……但是需要更多的输入与试验,和更少的移动鼠标与点击。
| 需要指出的是,在 DOM Inspector 中可以获得同样的内容,只不过用这种方法需要您更多 的操作,而不仅仅是点点鼠标而已。同 DOM Inspector 一样, Javascript Shell 并不会一直跟随您。如果您打开了 Javascript Shell, 然后又访问了其他的网页, Javascript Shell 将不能确定您要查看的对象。好的做法是,在您访问其他页面之前,先关闭 Javascript Shell;如果您还要使用 Javascript Shell,请您重新打开一次,而不是使用原来打开的那个。 |


![[首页]](/stylesheets/images/wiki.png)