【问题标题】:How to Convert this Greasemonkey code to JavaScript for Android?如何将此 Greasemonkey 代码转换为适用于 Android 的 JavaScript?
【发布时间】:2012-03-02 07:27:15
【问题描述】:

我正在尝试加载一个页面,然后在其上运行一个 javascript 代码,我发现了一个相同功能的 Greasemonkey 脚本,但是我在 android 中实现相同的东西时遇到了问题,可能是因为我对它一无所知javascript。

这是 Greasemonkey 脚本;它应该提供一个新链接:

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);


这就是我想要运行它的方式:

public void onPageFinished (WebView view, String url) {
                System.out.println("webview loaded");
                webView.loadUrl("javascript:/*...........Javascript code here........*/");

           }

关于如何获取该链接并在 web 视图中加载该页面的任何想法? 编辑:另一个版本做同样的事情。

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; 

【问题讨论】:

  • 您是否尝试过缩小该代码(在函数内),用%20 替换空格并将其放入链接中?
  • @kirilloid 不,我不知道这将如何工作,据我所知,此代码特定于用户脚本,因为 ex document.evaluate 仅适用于 firefox..

标签: javascript android greasemonkey android-webview userscripts


【解决方案1】:

好的,这里只是简单的 Xpath 查询,可以重写为 CSS 选择器。

我还决定替换window.location.href.match(/\?(.*)$/)[1]。如果我的版本不起作用,请将前 2 行替换为 var query = window.location.href.match(/\?(.*)$/)[1];

其实,也许var query = window.location.search.replace(/^\?/,'') 就足够了。

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);

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; 

压缩版:

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)");

【讨论】:

  • 首先感谢您的回答,但是我如何在webview中运行它,我能想到的唯一方法是将这段代码放在html或js文件中并加载该文件,但是这导致加载文件而不是注入的无限循环..??
  • 您在问题中描述了这一点! webView.loadUrl("javascript:/*one-lined js code here*/"); 打开javascript:-URL 不会重新加载页面。另外:developer.android.com/guide/webapps/…
  • @kirillorid 抱歉,我只是有点困惑,但这段代码确实有问题,它没有做任何事情,日志中也没有任何内容,我在脚本中添加了另一个版本它做同样的事情,但使用较旧的代码和不同的方法,也许这可以给你任何见解..
  • 语法错误:不在函数中返回。该代码应该在函数内部以及第一个版本中。
  • @kirillorid 我尝试做这样的事情 stackoverflow.com/questions/5649111/... 我仍然在 UNDEFINED 1 处收到 PHRASE ERROR。而且双引号似乎标志着 javascript 的结束,我该怎么做转义那些,“\\”不起作用,您可以编辑代码并将其放在函数中吗..?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 2020-05-07
  • 2017-10-04
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
相关资源
最近更新 更多