如何转换这个Greasemonkey的code为JavaScript为Android? [英] How to Convert this Greasemonkey code to JavaScript for Android?
问题描述
我试图加载一个页面,然后在其上运行一个javascript code,我发现了一个Greasemonkey的脚本不相同,但我实施的android一样的东西有问题,可能是因为我不了解JavaScript的东西。
这是Greasemonkey的脚本;它应该给予一个新的链接:
window.addEventListener(负荷,()的函数
{
VAR链接= document.evaluate(\"//div[@class='dl_startlink']/div/a[contains(@href,'\"+window.location.href.match(/\\?(.*)$/)[1]+\"')]\",文档,空,了XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,NULL);
如果(!link.snapshotLength)
返回;
location.href = link.snapshotItem(0).href;},FALSE);
结果
这是我要如何运行:
公共无效onPageFinished(的WebView视图,字符串URL){
的System.out.println(装的WebView);
webView.loadUrl(JavaScript的:/ * ...........的Javascript code这里........ * /); }
我如何得到该链接,并在web视图加载页面任何想法?
编辑:另一个版本做同样的事情。
VAR候选人=关于document.evaluate(// * [@类='dl_startlink'] /格,文档,空,了XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,NULL);
如果(!candidates.snapshotLength)
返回;
//最高zIndex的股利有*真正的*链接;其余的是无用的。
- 变种maxDiv = candidates.snapshotItem(0);
- 为(VAR I = 1; I< candidates.snapshotLength;我++)
- 如果(maxDiv.style.zIndex< candidates.snapshotItem(我).style.zIndex)
- maxDiv = candidates.snapshotItem(ⅰ);
- location.href = maxDiv.children [0] .href;
好吧,这里只是简单的XPath查询,这可能会被改写为CSS选择器。
此外,我决定更换 window.location.href.match(/ \\?(。*)$ /)[1]
。如果我的版本将无法正常工作,取代前两行用 VAR的查询= window.location.href.match(/ \\(*)$ /?)[1];
事实上,甚至 VAR的查询= window.location.search.replace(/ ^ \\?/,'')
就足够了。
window.addEventListener(负荷,()的函数
{
变种L = window.location的;
VAR的查询= l.search? (?l.search.replace(/ ^ \\ /,'')+ l.hash): VAR链接= document.querySelector(div.dl_startlink> DIV>将[HREF ='+查询+']);
如果回报(链接!);
l.href = link.href;
},FALSE);
新的code为Android:
VAR候选人= document.querySelector(div.dl_startlink>的div);
如果(!candidates.length)
返回;
//最高zIndex的股利有*真正的*链接;其余的是无用的。
变种maxDiv =候选人[0];
对于(VAR I = 1; I< candidates.length;我++)
如果(maxDiv.style.zIndex<考生[I] .style.zIndex)
maxDiv =考生[I]
location.href = maxDiv.children [0] .href;
压缩版本:
<$p$p><$c$c>webView.loadUrl(\"javascript:window.addEventListener('load',function(){var%20candidates=document.querySelector('div.dl_startlink>div');if(!candidates.length)return;var maxDiv=candidates[0];for(var%20i=1;i<candidates.length;i++)if(maxDiv.style.zIndex<candidates[i].style.zIndex)maxDiv=candidates[i];location.href=maxDiv.children[0].href;},false)\");I am trying to load a page and then run a javascript code on it, I found a Greasemonkey script that does the same, but I am having problems implementing the same thing in android, probably because I don't know anything about javascript.
This is the Greasemonkey script; it's supposed to a give a new link:
window.addEventListener("load", function ()
{
var link = document.evaluate("//div[@class='dl_startlink']/div/a[contains(@href,'"+window.location.href.match(/\?(.*)$/)[1]+"')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if( !link.snapshotLength )
return;
location.href = link.snapshotItem(0).href;
}, false);
and this is how I want to run it:
public void onPageFinished (WebView view, String url) {
System.out.println("webview loaded");
webView.loadUrl("javascript:/*...........Javascript code here........*/");
}
Any ideas on how I get that link and load that page in the webview? EDIT: Another version does the same thing.
var candidates = document.evaluate("//*[@class = 'dl_startlink']/div", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if( !candidates.snapshotLength )
return;
//The DIV with the highest zIndex has the *real* link; the rest are useless.
- var maxDiv = candidates.snapshotItem(0);
- for( var i = 1; i < candidates.snapshotLength; i++ )
- if( maxDiv.style.zIndex < candidates.snapshotItem(i).style.zIndex )
- maxDiv = candidates.snapshotItem(i);
- location.href = maxDiv.children[0].href;
Ok, here's only simple Xpath query, which may be rewritten as CSS selector.
Also I decided to replace window.location.href.match(/\?(.*)$/)[1]
. If my version will not work, replace first 2 lines with var query = window.location.href.match(/\?(.*)$/)[1];
.
Actually, maybe even var query = window.location.search.replace(/^\?/,'')
is enough.
window.addEventListener("load", function ()
{
var l = window.location;
var query = l.search ? (l.search.replace(/^\?/,'') + l.hash) : ""
var link = document.querySelector("div.dl_startlink > div > a[href='" + query + "']");
if (!link) return;
l.href = link.href;
}, false);
New code for Android:
var candidates = document.querySelector("div.dl_startlink > div");
if( !candidates.length)
return;
//The DIV with the highest zIndex has the *real* link; the rest are useless.
var maxDiv = candidates[0];
for( var i = 1; i < candidates.length; i++ )
if( maxDiv.style.zIndex < candidates[i].style.zIndex )
maxDiv = candidates[i];
location.href = maxDiv.children[0].href;
Compacted version:
webView.loadUrl("javascript:window.addEventListener('load',function(){var%20candidates=document.querySelector('div.dl_startlink>div');if(!candidates.length)return;var maxDiv=candidates[0];for(var%20i=1;i<candidates.length;i++)if(maxDiv.style.zIndex<candidates[i].style.zIndex)maxDiv=candidates[i];location.href=maxDiv.children[0].href;},false)");
这篇关于如何转换这个Greasemonkey的code为JavaScript为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!