Best way to interpret JavaScript in an Android Java Application -


i need way java app regex-based string analysis , replacement. each replacement rule, , app should capable of reading file containing these rules. allow users download sets of rules, , development of them sped considerably way, since app not need recompiled each new or changed rule.

here example rules executed server-side in python

    # ---------- copy ----------     title = item['title']     uri = item['action']['uri']      # ---------- spiegel online ----------     title = title.replace(" - spiegel online - nachrichten", "").replace(" - spiegel online", "")      if domain == "m.spiegel.de":       uri = "http://www.spiegel.de" + uri[19:]      if domain == "spon.de":       r = requests.head(uri) # <----- resolve url       try:    uri = r.headers['location']       except: traceback.print_exc()      # ---------- stack overflow ----------     if title.endswith(" - stack overflow"):       title = title[:-17]      # ---------- google play ----------     if uri.startswith("https://play.google.com"):       match = re.search(u'^das könnte interessant sein: "(.+)"$', title, re.dotall)       if match:         title = match.group(1)      # ---------- prime guide tv ----------     if "@primeguidetv" in uri:       uri_segments = uri.split("\n")       when = uri_segments[1].split(", ")       when_times = when[1].split(" - ")       dtfrom = datetime.datetime.strptime(when[0]+when_times[0], "%d.%m.%y%h:%m")       dtto   = datetime.datetime.strptime(when[0]+when_times[1], "%d.%m.%y%h:%m")       title += " -- " + dtfrom.strftime("%h:%m -- %a %d %b") + " -- " + when[2].strip()# + " -- " + str(int((dtto - dtfrom).total_seconds() / 60)) + "min" + " -- " + uri_segments[1]       uri = uri_segments[2]      # ---------- wikipedia, enforce https , demobilize ----------     if " - wikipedia, " in title:       title = title[:title.find(" - wikipedia, ")]       uri = re.sub(r"https?://(en\.)(?:m\.)?(wikipedia\.org/.+)", r"https://\1\2", uri, 0, re.dotall)      # ---------- youtube ----------     if domain == "youtu.be":       r = requests.head(uri) # <----- resolve url       try:    uri = r.headers['location'].replace('&feature=youtu.be', '')       except: traceback.print_exc()     match = re.search(u'^schau dir "(.+)" auf youtube an$', title, re.dotall)     if match:       title = match.group(1)      # ---------- update ----------     item['title'] = title     item['action']['uri'] = uri     #print '--', title.encode('utf-8'), '--', uri 

considering requirements of title , uri parsing change rapidly, think it's best offload entire task interpreter, instead of trying find method express in java. hard try done above prime guide tv via flexibe java code.

i thought using webview , pushing rules javascript text webview, can work on text, , retrieve result. no gui required, , in cases activity have theme.nodisplay, of don't know if cause trouble.

i've read bit rhino, may possible option, don't know if overhead bit big.

is there better way accomplish this? worth trying access internal v8 engine, i've read in posts, or problem regarding compatibility?

basically, have following options:

  1. use invisible webview. pros: easiest approach start with. can use injected java objects (via webview.addjavascriptinterface) interacting between js <-> java. js executed on v8, runs fast. cons: high memory costs (webview full-blown browser engine), js <-> java bridge on android kitkat+ has significant overhead if need perform thousands of calls per second.

  2. run on java vm. can run either javascript or python on java vm. pros: no native libraries needed, js / python <-> java interaction trivially simple, have full access java classes js code. cons: js / python execution slower on native engine, if need pure performance, isn't way.

  3. package v8 yourself. unfortunately, it's not possible re-use v8 webview without doing gross , fragile hacks, instead need package native library , distribute apk (and deal both 32-bit , 64-bit devices). need implement own (or re-use else's) js<->java bindings. lot of work feasible. pros: speeeed! cons: technically challenging, standalone v8 there no javascript debugger, because webview devtools remote debugging implemented in rendering engine (called blink).


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -