Notalib / nativescript-webview-ext

Nativescript plugin with extended WebView functionality
Apache License 2.0
76 stars 37 forks source link

iOS: x-local doesn't work on remote pages over https. #70

Open m-abs opened 4 years ago

m-abs commented 4 years ago

WKWebView considers the x-local scheme insecure, when the page is loaded via HTTPS and refuses to load local files via x-local.

Workaround: Use autoLoadJavaScriptFile or autoLoadStyleSheetFile before loading the webpage. This will generate at WKUserScript that doesn't have the same restrictions.

Alternativ for javascript files.

            const elId = resourceName.replace(/^[:]*:\/\//, "").replace(/[^a-z0-9]/g, "");
            const sourceCode = await fs.File.fromPath(this.resolveLocalResourceFilePath(path) as string).readText();

             const scriptCode = `window.nsWebViewBridge.injectJavaScript(${JSON.stringify(elId)}, ${JSON.stringify(sourceCode)});`;

            webview.executeJavaScript(scriptCode, false);

Alternative for CSS files.

            const elId = resourceName.replace(/^[:]*:\/\//, "").replace(/[^a-z0-9]/g, "");
            const sourceCode = await fs.File.fromPath(this.resolveLocalResourceFilePath(path) as string).readText();

             const scriptCode = `window.nsWebViewBridge.injectStyleSheet(${JSON.stringify(elId)}, ${JSON.stringify(sourceCode)});`;

            webview.executeJavaScript(scriptCode, false);

No known workaround for other filetypes.

I'm working on a solution, but I cannot promise when I can have it solved.

ray007 commented 4 years ago

I'm having a problem with loading data from x-local with the contents of my webview being loaded from file://. And the x-local load fails silently, neither onload nor onerror of the XmlHttpRequest seem to get called. Is this the same error?

Directly loading the data with file:// from a subfolder of knownFolders.documents() also didn't work

Edit: onreadystatechange() does work, so not the same problem. Edit2: fetch() does not work