diff options
author | mitsugu oyama | 2010-11-15 13:22:14 +0900 |
---|---|---|
committer | mitsugu oyama | 2010-11-15 13:22:14 +0900 |
commit | debbca424278eaeb66a7d03d27f8cfd9f6a42651 (patch) | |
tree | cb5c7b1f25f1933e7d726e91cb3758ea70b0cee0 /onclick.js | |
parent | da3680fb80f098a7fb0390aa2391a623d2e43385 (diff) | |
download | vimperator-plugins-debbca424278eaeb66a7d03d27f8cfd9f6a42651.tar.bz2 |
add onclick.js
Diffstat (limited to 'onclick.js')
-rw-r--r-- | onclick.js | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/onclick.js b/onclick.js new file mode 100644 index 0000000..d975efc --- /dev/null +++ b/onclick.js @@ -0,0 +1,86 @@ +// INFO // +var INFO = +<plugin name="onclick.js" version="0.1" + summary="Emulate onClick event." + href="http://github.com/vimpr/vimperator-plugins/blob/master/onclick.js" + xmlns="http://vimperator.org/namespaces/liberator"> + <author email="mitsugu.oyama@gmail.com">Mitsugu Oyama</author> + <license href="http://opensource.org/licenses/mit-license.php">MIT</license> + <project name="Vimperator" minVersion="2.3"/> + <p> + Emulate onClick event. + </p> + <item> + <tags>'onclick'</tags> + <spec>:onclick <a>target</a></spec> + <description> + <p>Emulate onClick event.</p> + <p>Should add hash table to .vimperatorrc.</p> + <code><![CDATA[ +For Exsample, + js <<EOM + liberator.globalVariables.onclickTable={ + 'github':'//span[@class="toggle"][1]', + 'pixiv':'//span[@class="trigger"][1]' + }; + EOM + ]]></code> + </description> + </item> +</plugin>; + +commands.addUserCommand( + ['onclick'], + 'Emulate onClick event.', + function(args){ + if(args.length<1){ + liberator.echoerr('Usage: onclick {xpath_id}'); + return false; + } + let strXpath=liberator.globalVariables.onclickTable[args[0]]; + if(undefined==strXpath){ + liberator.echoerr('Not Found XPath'); + return false; + } + let contents=gBrowser.selectedBrowser.contentDocument; + let evaluateXPath=function(aNode,aExpr){ + let xpe=new XPathEvaluator(); + let nsResolver=xpe.createNSResolver(aNode.ownerDocument==null ? + aNode.documentElement : aNode.ownerDocument.documentElement); + let result=xpe.evaluate(aExpr,aNode,nsResolver,0,null); + let found=[]; + let res; + while(res=result.iterateNext()) + found.push(res); + return found; + }; + let elms=evaluateXPath(contents,strXpath); + if(elms.length==0){ + liberator.echoerr('Not Found Element. Can you check it?'); + return false; + } + let evt=contents.createEvent("MouseEvents"); + evt.initMouseEvent( + 'click', + true, // canBubble + true, // cancelable + window, // view + 0, // detail + 0, // screenX + 0, // screenY + 0, // clientX + 0, // clientY + false, // ctrlKey + false, // altKey + false, // shiftKey + false, // metaKey + 0, // button + null //relatedTarget + ); + elms[0].dispatchEvent(evt); + }, + { + literal: false + }, + true +); |