Skip to content

Commit

Permalink
browser: Fixing reading of the injection script
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Jul 23, 2020
1 parent 4f6a372 commit 6a7b563
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<target name="compile" depends="clean">
<mkdir dir="build"/>
<mkdir dir="build/main"/>
<javac srcdir="src/main/java" destdir="build/main" source="1.8" target="1.8">
<javac srcdir="src/main/java" destdir="build/main" source="1.8" target="1.8" debug="true" debuglevel="lines,vars,source">
<classpath refid="classpath"/>
</javac>
</target>
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/org/luwrain/interaction/javafx/browser/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.luwrain.interaction.javafx.browser;

import java.io.*;
import java.util.*;

import javafx.beans.value.ObservableValue;
Expand All @@ -37,6 +38,7 @@

abstract class Base
{
static private final String RESCAN_RESOURCE_PATH = "org/luwrain/interaction/javafx/injection.js";
static final String LOG_COMPONENT = "web";

/*
Expand All @@ -62,12 +64,11 @@ static final class DomScanResult
protected JSObject injectionRes = null;
protected JSObject jsWindow = null;

protected Base(BrowserEvents events, String injectedScript)
protected Base(BrowserEvents events) throws IOException
{
NullCheck.notNull(injectedScript, "injectedScript");
NullCheck.notNull(events, "events");
Utils.ensureFxThread();
this.injectedScript = injectedScript;
this.injectedScript = readInjectedScript();
this.webView = new WebView();
this.webEngine = webView.getEngine();
this.webView.setOnKeyReleased((event)->onKeyReleased(event));
Expand Down Expand Up @@ -215,6 +216,25 @@ private void onKeyReleased(KeyEvent event)
}
}

private String readInjectedScript() throws IOException
{
final InputStream is = getClass().getClassLoader().getResourceAsStream(RESCAN_RESOURCE_PATH);
if (is == null)
throw new IOException("No resource with the path 'RESCAN_RESOURCE_PATH'");
final BufferedReader r = new BufferedReader(new InputStreamReader(is));
try {
final StringBuilder b = new StringBuilder();
String line = null;
while((line = r.readLine()) != null)
b.append(line + "\n");
return new String(b);
}
finally {
r.close();
is.close();
}
}

static long jsLong(Object o)
{
if(o == null)
Expand All @@ -232,5 +252,4 @@ DomScanResult getDomScanResult()
{
return this.domScanRes;
}

}
29 changes: 2 additions & 27 deletions src/main/java/org/luwrain/interaction/javafx/browser/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@

public final class Browser extends Base implements org.luwrain.browser.Browser
{
static private final String RESCAN_RESOURCE_PATH = "org/luwrain/interaction/javafx/injection.js";
static final int LAST_MODIFIED_SCAN_INTERVAL = 100; // lastModifiedTime rescan interval in milliseconds
static final String LUWRAIN_NODE_TEXT="luwrain_node_text"; // javascript window's property names for using in executeScrypt

private final JavaFxInteraction interaction;

public Browser(JavaFxInteraction interaction, BrowserEvents events)
public Browser(JavaFxInteraction interaction, BrowserEvents events) throws IOException
{
super(events, readTextResource(RESCAN_RESOURCE_PATH));
super(events);
NullCheck.notNull(interaction, "interaction");
this.interaction = interaction;
}
Expand Down Expand Up @@ -139,28 +138,4 @@ public Browser(JavaFxInteraction interaction, BrowserEvents events)
return 0;
return domScanRes.dom.size();
}

static String readTextResource(String path)
{
NullCheck.notEmpty(path, "path");
try {
final InputStream s = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
if (s == null)
{
Log.error(LOG_COMPONENT, "inaccessible resource:" + path);
return null;
}
final BufferedReader r = new BufferedReader(new InputStreamReader(s));
final StringBuilder b = new StringBuilder();
String line = null;
while((line = r.readLine()) != null)
b.append(line + "\n");
return new String(b);
}
catch(IOException e)
{
Log.error(LOG_COMPONENT, "unable to read system resource:" + path + ":" + e.getClass().getName() + ":" + e.getMessage());
return null;
}
}
}

0 comments on commit 6a7b563

Please sign in to comment.