Notalib / nativescript-webview-ext

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

Can't `loadJavaScriptFile()` #38

Closed artemave closed 5 years ago

artemave commented 5 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

I am trying to inject smooth-scroll into the page with the following code:

  webview.on(WebViewExt.loadFinishedEvent, async (args: EventData) => {
    await webview.loadJavaScriptFile('smooth-scroll.js', '~/assets/smooth-scroll.js')
    await webview.executeJavaScript('window.firetabScroll = new SmoothScroll()')

But I get this error: image

note: ~/assets/smooth-scroll.js is copied from node_modules/smooth-scroll/dist/smooth-scroll.js into app/assets

Possibly relevant logs:

chromium: [INFO:library_loader_hooks.cc(36)] Chromium logging enabled: level = 0, default verbosity = 0
JS: https://chords-and-tabs.net/song/name/manowar-carry-on https://chords-and-tabs.net/song/name/manowar-carry-on
chromium: [ERROR:gl_surface_egl.cc(335)] eglChooseConfig failed with error EGL_SUCCESS
chromium: [ERROR:gl_surface_egl.cc(335)] eglChooseConfig failed with error EGL_SUCCESS
chromium: [INFO:CONSOLE(229)] "The deviceorientation events are blocked by feature policy. See https://github.com/WICG/feature-policy/blob/master/features.md#sensor-features", source: https://www.youtube.com/yts/jsbin/player_ias-vfl7A4uZG/en_US/base.js (229)
chromium: [WARNING:web_contents_impl.cc(4288)] https://chords-and-tabs.net/ ran insecure content from x-local://smooth-scroll.js
chromium: [INFO:CONSOLE(1)] "Mixed Content: The page at 'https://chords-and-tabs.net/song/name/manowar-carry-on' was loaded over HTTPS, but requested an insecure script 'x-local://smooth-scroll.js'. This content should also be served over HTTPS.", source:  (1)
chromium: [INFO:CONSOLE(1)] "Failed to load x-local://smooth-scroll.js - error: [object Event]", source:  (1)
chromium: [INFO:CONSOLE(1)] "xlocalsmoothscrolljs already exists", source:  (1)
chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: SmoothScroll is not defined", source: https://chords-and-tabs.net/song/name/manowar-carry-on (1)

Looks like I am doing something wrong, but I can't figure out what.

m-abs commented 5 years ago

Hey,

Are you sure the app/assets are added to the app? You have to update webpack.config.js to copy the assets-folder to the app-bundle.

            new CopyWebpackPlugin([
                { from: { glob: "fonts/**" } },
                { from: { glob: "**/*.jpg" } },
                { from: { glob: "**/*.png" } },
                { from: { glob: 'assets/**'} }, // add this
            ])

If the assets are added and it still doesn't work, you can enable tracing with this:

import * as trace from 'tns-core-modules/trace';

trace.setCategories('NOTA');
trace.enable();

and give me the output.

artemave commented 5 years ago

Ah! Spot on - I forgot about webpack. It works now. Thank you!