Spider-Crack-JS icon indicating copy to clipboard operation
Spider-Crack-JS copied to clipboard

豆瓣搜索的js在JAVA的ScrtptEngine中会报错 t.slice is not a function 附解决方法

Open InfoGG opened this issue 11 months ago • 0 comments

测试代码

@Test
public void test() {
    try {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("JavaScript");
        String jsScript = FileUtils.readFileToString(
                ResourceUtils.getFile("classpath:douban_decrypt.js"), Charset.defaultCharset());
        engine.eval(jsScript);
        Invocable jsEngine = (Invocable) engine;
        String s = "";//copy from window__DATA__
        Object result = jsEngine.invokeFunction("decrypt", s);
        System.out.println(result);
    } catch (IOException | ScriptException | NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}

javax.script.ScriptException: TypeError: t.slice is not a function in <eval> at line number 1151

原因是Nashorn engine似乎不支持uint8Array的slice方法,需要在js脚本中做如下改动:

if (!Uint8Array.prototype.slice) {
    Uint8Array.prototype.slice = function () {
        return new Uint8Array(this).subarray(this.arguments);
    }
}

贴在function decrypt(r)的开头处即可

InfoGG avatar Mar 08 '24 11:03 InfoGG