/cordova-plugin-wkuserscript

Cordova plugin that allows passing some JS code to be added to all frames via WKUserScript

Primary LanguageObjective-CApache License 2.0Apache-2.0

title description
WKUserScript
Plugin to add WKUserScripts in WKWebView (iOS).

cordova-plugin-wkuserscript

Plugin to add WKUserScripts in WKWebView (iOS). This can be used to inject JS code to all iframes of your app, for example to intercept window.open calls.

Please notice that this plugin requires you to use WKWebView.

Installation

This plugin isn't published in npm yet, so it must be installed via repo url:

cordova plugin add  https://github.com/moodlemobile/cordova-plugin-wkuserscript

Methods

This plugin defines global WKUserScript object.

Although in the global scope, it is not available until after the deviceready event.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(WKUserScript);
}
  • WKUserScript.addScript

WKUserScript.addScript

Adds a user script that will be injected to all iframes of the app (and also to the WebView, be careful with that). You can either pass the JS code to inject or the path of a JS script to inject.

Supported Platforms

  • iOS 11+

Quick Example

WKUserScript.addScript({
    code: 'window.myGlobalVar = "Test";',
}).then(function() {
    // Success.
}).catch(function() {
    // Error.
});