aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhogelog2009-02-02 13:02:07 +0000
committerhogelog2009-02-02 13:02:07 +0000
commitf5e8cb97deccbbca25f948878f9e5f3985324e5b (patch)
tree0bc285e029e6705e1053ed976badcad82e36907e
parent2901164feecc922c81e37ddaf223c7447e08816d (diff)
downloadvimperator-plugins-f5e8cb97deccbbca25f948878f9e5f3985324e5b.tar.bz2
add xpath_hint.js
* add "get element's XPath" hint mode git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@29440 d0d07461-0603-4401-acd4-de1884942a52
-rw-r--r--xpath_hint.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/xpath_hint.js b/xpath_hint.js
new file mode 100644
index 0000000..febba50
--- /dev/null
+++ b/xpath_hint.js
@@ -0,0 +1,63 @@
+/*** BEGIN LICENSE BLOCK {{{
+ Copyright (c) 2009 hogelog<konbu.komuro@gmail.com>
+
+ distributable under the terms of an MIT-style license.
+ http://www.opensource.jp/licenses/mit-license.html
+}}} END LICENSE BLOCK ***/
+// PLUGIN_INFO//{{{
+var PLUGIN_INFO =
+<VimperatorPlugin>
+ <name>{NAME}</name>
+ <description>UUID generator</description>
+ <author mail="konbu.komuro@gmail.com" homepage="http://d.hatena.ne.jp/hogelog/">hogelog</author>
+ <version>0.1.0</version>
+ <minVersion>2.0pre</minVersion>
+ <maxVersion>2.0pre</maxVersion>
+ <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/xpath_hint.js</updateURL>
+ <license>MIT</license>
+ <detail><![CDATA[
+
+== MAPPING ==
+;x:
+ copy selected element's XPath
+
+]]></detail>
+</VimperatorPlugin>;
+//}}}
+
+(function(){
+const DEFAULT_MAP = "x";
+
+let xpathHintMap = liberator.globalVariables.xpath_hint_map || DEFAULT_MAP;
+
+let xh = plugins.xpath_hint = {
+ getElementXPath: function(elem)
+ {
+ if (elem.nodeType == 9) { // DOCUMENT_NODE = 9
+ return "";
+ }
+ if (elem.hasAttribute("id")) {
+ return '//*[@id="'+elem.getAttribute("id")+'"]';
+ }
+ let name = elem.tagName.toLowerCase();
+ let parent = elem.parentNode;
+ let path = arguments.callee(parent)+"/"+name;
+ let children = Array.filter(parent.childNodes, function(e) e.nodeName == elem.nodeName && e.nodeType == elem.nodeType);
+
+ if (children.length != 1 && children[0]!=elem) {
+ path += "["+(children.indexOf(elem)+1)+"]";
+ }
+ return path;
+ },
+ addMode: function(mode, prompt, action, tags)
+ {
+ hints.addMode(mode, prompt,
+ function(e) action(xh.getElementXPath(e)), tags);
+ },
+};
+xh.addMode(xpathHintMap, "copy xpath",
+ function(xpath) util.copyToClipboard(xpath, true),
+ function() "//*");
+
+})();
+// vim: set fdm=marker sw=4 ts=4 et: