title: WKUserScript
description: 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
You can install the plugin using the following command:
cordova plugin add cordova-plugin-wkuserscript
Methods
This plugin defines global WKUserScript
object.
Although in the global scope, it is not available until after the deviceready
event.
function onDeviceReady() {
console.log(WKUserScript);
}
document.addEventListener("deviceready", onDeviceReady, false);
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
Quick Example
WKUserScript.addScript({
code: 'window.myGlobalVar = "Test";',
}).then(function() {
}).catch(function() {
});