| 1 | let PLUGIN_INFO = |
|---|
| 2 | <VimperatorPlugin> |
|---|
| 3 | <name>{NAME}</name> |
|---|
| 4 | <description>DOM Inspector command</description> |
|---|
| 5 | <require type="extension" id="inspector@mozilla.org">DOM Inspector</require> |
|---|
| 6 | <author mail="teramako@gmail.com" homepage="http://vimperator.g.hatena.ne.jp/teramako/">teramako</author> |
|---|
| 7 | <version>0.2</version> |
|---|
| 8 | <minVersion>2.0pre</minVersion> |
|---|
| 9 | <maxVersion>2.0</maxVersion> |
|---|
| 10 | <detail><![CDATA[ |
|---|
| 11 | == Usage == |
|---|
| 12 | :inspect #{id}: |
|---|
| 13 | inspect the element of the {id} in browser content |
|---|
| 14 | :inspect! #{id}: |
|---|
| 15 | inspect the element of the {id} in firefox |
|---|
| 16 | :inspect[!] -f[rame] #{id}: |
|---|
| 17 | inspect the document in the frame element of the {id} |
|---|
| 18 | :inspect {str}: |
|---|
| 19 | inspect the return value of evaluated the {str} |
|---|
| 20 | ]]></detail> |
|---|
| 21 | </VimperatorPlugin>; |
|---|
| 22 | |
|---|
| 23 | (function(){ |
|---|
| 24 | |
|---|
| 25 | const inspectorID = "inspector@mozilla.org"; |
|---|
| 26 | if (!Application.extensions.has(inspectorID) || !Application.extensions.get(inspectorID).enabled) return; |
|---|
| 27 | |
|---|
| 28 | /* これやるとFirefox終了時に実行されるんだけど...なぜ? -> Ubiquityが悪さしているみたい |
|---|
| 29 | Object.prototype.inspect = function(){ |
|---|
| 30 | runInspector(this); |
|---|
| 31 | }; |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | function runInspector(node){ |
|---|
| 35 | if (node instanceof Document){ |
|---|
| 36 | inspectDOMDocument(node); |
|---|
| 37 | } else if (node instanceof Node){ |
|---|
| 38 | inspectDOMNode(node); |
|---|
| 39 | } else if (node !== null && typeof node != "undefined"){ |
|---|
| 40 | inspectObject(node); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | function getIDList(filter, isChrome){ |
|---|
| 45 | var doc = isChrome ? document : content.document; |
|---|
| 46 | var iter = buffer.evaluateXPath('//*[@id and contains(@id,"' + filter + '")]',doc); |
|---|
| 47 | return [["#" + e.id, "TagName: "+ e.tagName] for (e in iter)]; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | var options = [ |
|---|
| 51 | [["-frame","-f"], commands.OPTION_NOARG] |
|---|
| 52 | ]; |
|---|
| 53 | commands.addUserCommand(["inspect","dominspect"],"run DOM Inspector", |
|---|
| 54 | function(args){ |
|---|
| 55 | var arg = args[0]; |
|---|
| 56 | var doc = args.bang ? document : content.document; |
|---|
| 57 | var node; |
|---|
| 58 | if (!arg){ |
|---|
| 59 | node = doc; |
|---|
| 60 | } else if (arg.charAt(0) == "#"){ |
|---|
| 61 | let id = arg.substr(1); |
|---|
| 62 | node = doc.getElementById(id); |
|---|
| 63 | if (!node){ |
|---|
| 64 | liberator.echoerr("No such id: " + id ); |
|---|
| 65 | return; |
|---|
| 66 | } |
|---|
| 67 | } else { |
|---|
| 68 | try { |
|---|
| 69 | node = liberator.eval(args.string); |
|---|
| 70 | } catch (e){ |
|---|
| 71 | liberator.echoerr(e); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | if (args["-frame"] && node.contentDocument) node = node.contentDocument; |
|---|
| 75 | runInspector(node); |
|---|
| 76 | },{ |
|---|
| 77 | bang: true, |
|---|
| 78 | argCount: "*", |
|---|
| 79 | options: options, |
|---|
| 80 | completer: function(context, args){ |
|---|
| 81 | if (args[0] && args[0].charAt(0) == "#"){ |
|---|
| 82 | var arg = args[0]; |
|---|
| 83 | var list = getIDList(arg.substr(1), args.bang); |
|---|
| 84 | context.completions = list.filter(function(elem) elem[0].indexOf(arg) == 0); |
|---|
| 85 | } else { |
|---|
| 86 | completion.javascript(context); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | ); |
|---|
| 91 | |
|---|
| 92 | })(); |
|---|
| 93 | |
|---|