Skip to content

Commit

Permalink
Remove setUserAgent, and integrate behavior with addHeaders #7
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Mar 6, 2018
1 parent e9268aa commit 28d3f72
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions demo/app/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<ScrollView>
<StackLayout class="p-20">

<Label text="Default headers, according to https://httpbin.org/headers:" class="t-20 text-center c-black" textWrap="true"/>
<WebView height="320" src="https://httpbin.org/headers?wv=1"/>
<!--<Label text="Default headers, according to https://httpbin.org/headers:" class="t-20 text-center c-black" textWrap="true"/>-->
<!--<WebView height="320" src="https://httpbin.org/headers?wv=1"/>-->

<Label text="And now with custom headers:" class="t-20 text-center c-black" textWrap="true"/>
<WebView loaded="addHeaders" loadStarted="webViewLoadStarted" loadFinished="webViewLoadFinished" height="370" src="whttps://httpbin.org/headers?wv=2"/>
<WebView loaded="webViewLoaded" loadStarted="webViewLoadStarted" loadFinished="webViewLoadFinished" height="370" src="https://httpbin.org/headers?wv=2"/>

</StackLayout>
</ScrollView>
Expand Down
7 changes: 3 additions & 4 deletions demo/app/tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ describe("addHeaders", () => {
WebViewUtils.addHeaders(webView, headers);

// load a website
// (<any>webView)._loadUrl("https://httpbin.org/headers?testing=schmesting");
(<any>webView)._loadUrl("https://www.nu.nl");
(<any>webView)._loadUrl("https://httpbin.org/headers?testing=schmesting");

setTimeout(() => {
// if both events fired once, it's ok
expect(loadStartedCounter).toEqual(1);
expect(loadFinishedCounter).toEqual(1);
expect(loadStartedCounter).toBeGreaterThanOrEqual(1);
expect(loadFinishedCounter).toBeGreaterThanOrEqual(1);
done();
}, 4000);
});
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-webview-utils",
"version": "2.0.0",
"version": "2.0.1",
"description": "Add custom headers to a NativeScript WebView. Perhaps more utils later.",
"main": "webview-utils",
"typings": "index.d.ts",
Expand Down
22 changes: 13 additions & 9 deletions src/webview-utils.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ export class WebViewUtils extends android.webkit.WebViewClient {
public static addHeaders(wv: WebView, headers: Map<string, string>) {
WebViewUtils.wv = wv;
WebViewUtils.headers = headers;
// Wrap this in a timeout for Android, otherwise webview.android might not be initialized
setTimeout(() => {
// Conditionally wrap this in a timeout for Android, otherwise webview.android might not be initialized
if ((<any>wv).android) {
(<any>wv).android.setWebViewClient(WebViewUtils.initWithView(wv));
});
} else {
setTimeout(() => {
(<any>wv).android.setWebViewClient(WebViewUtils.initWithView(wv));
});
}
}

private static initWithView(view: WebView): WebViewUtils {
Expand All @@ -38,7 +42,7 @@ export class WebViewUtils extends android.webkit.WebViewClient {
// Note that this method is overloaded in Java (changed in Lollipop - no longer used, from the looks of it)
public shouldOverrideUrlLoading(webView: android.webkit.WebView, urlOrWebResourceRequest: any /* string | android.webkit.WebResourceRequest */): boolean {
const url = typeof urlOrWebResourceRequest === "string" ? urlOrWebResourceRequest : urlOrWebResourceRequest.getUrl().toString();
(<any>webView).loadUrl(url, this.getAdditionalHeadersForUrl(webView, url));
(<any>webView).loadUrl(url, this.getAdditionalHeadersForUrl(url));
return true;
}

Expand All @@ -47,9 +51,9 @@ export class WebViewUtils extends android.webkit.WebViewClient {
const headersAdded = this.headersAddedTo.has(url);
if (url.indexOf("http") === 0 && !headersAdded) {
++this.startEventCount;
webView.loadUrl(url, this.getAdditionalHeadersForUrl(webView, url));
this._view.android.loadUrl(url, this.getAdditionalHeadersForUrl(url));
} else if (headersAdded && WebViewUtils.wv && url.indexOf("http") === 0) {
if (++this.startEventCount === 3) {
if (++this.startEventCount === 2) {
onLoadStarted(WebViewUtils.wv, url, undefined);
}
}
Expand All @@ -60,7 +64,7 @@ export class WebViewUtils extends android.webkit.WebViewClient {
if (url.indexOf("http") === -1) {
return;
}
if (WebViewUtils.wv && this.startEventCount === 3) {
if (WebViewUtils.wv && this.startEventCount > 1) {
onLoadFinished(WebViewUtils.wv, url, undefined);
}
}
Expand Down Expand Up @@ -90,13 +94,13 @@ export class WebViewUtils extends android.webkit.WebViewClient {
}
}

private getAdditionalHeadersForUrl(webView: android.webkit.WebView, url: string): java.util.Map<String, String> {
private getAdditionalHeadersForUrl(url: string): java.util.Map<String, String> {
const headers: java.util.Map<String, String> = new java.util.HashMap();
if (!this.headersAddedTo.has(url)) {
WebViewUtils.headers.forEach((val, key) => {
headers.put(key, val);
if (key.toLowerCase() === "user-agent") {
webView.getSettings().setUserAgentString(val);
this._view.android.getSettings().setUserAgentString(val);
}
});
this.headersAddedTo.add(url);
Expand Down

0 comments on commit 28d3f72

Please sign in to comment.