@codebrew/capacitor-user-agent
Advanced tools
| package codebrew.capacitor.plugins.useragent; | ||
| import android.webkit.WebSettings | ||
| import com.getcapacitor.JSObject | ||
| import com.getcapacitor.Plugin | ||
| import com.getcapacitor.PluginCall | ||
| import com.getcapacitor.PluginMethod | ||
| import com.getcapacitor.annotation.CapacitorPlugin | ||
| @CapacitorPlugin(name = "UserAgent") | ||
| class UserAgentPlugin : Plugin() { | ||
| private var settings: WebSettings? = null | ||
| override fun load() { | ||
| settings = bridge.webView.settings | ||
| } | ||
| @PluginMethod | ||
| fun get(call: PluginCall) { | ||
| val ret = JSObject() | ||
| val userAgent = settings?.userAgentString | ||
| ret.put("userAgent", userAgent) | ||
| call.resolve(ret) | ||
| } | ||
| @PluginMethod | ||
| fun set(call: PluginCall) { | ||
| val value = call.getString("userAgent", null) | ||
| settings?.userAgentString = value | ||
| val ret = JSObject() | ||
| ret.put("userAgent", settings?.userAgentString) | ||
| call.resolve(ret) | ||
| } | ||
| @PluginMethod | ||
| fun reset(call: PluginCall) { | ||
| settings!!.userAgentString = null | ||
| val ret = JSObject() | ||
| call.resolve() | ||
| } | ||
| } | ||
@@ -9,2 +9,3 @@ ext { | ||
| buildscript { | ||
| ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '1.8.21' | ||
| repositories { | ||
@@ -16,2 +17,3 @@ google() | ||
| classpath 'com.android.tools.build:gradle:8.0.2' | ||
| classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | ||
| } | ||
@@ -62,2 +64,4 @@ } | ||
| androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" | ||
| implementation "androidx.core:core-ktx:1.10.1" | ||
| implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
| } |
+1
-1
| { | ||
| "name": "@codebrew/capacitor-user-agent", | ||
| "version": "0.0.5", | ||
| "version": "0.0.6", | ||
| "description": "User Agent get/set/reset plugin for Capacitor", | ||
@@ -5,0 +5,0 @@ "main": "dist/plugin.cjs.js", |
| package codebrew.capacitor.plugins.useragent; | ||
| import android.webkit.WebSettings; | ||
| import com.getcapacitor.JSObject; | ||
| import com.getcapacitor.Plugin; | ||
| import com.getcapacitor.PluginCall; | ||
| import com.getcapacitor.PluginMethod; | ||
| import com.getcapacitor.annotation.CapacitorPlugin; | ||
| @CapacitorPlugin(name = "UserAgent") | ||
| public class UserAgentPlugin extends Plugin { | ||
| private WebSettings settings; | ||
| @Override | ||
| public void load() { | ||
| settings = bridge.getWebView().getSettings(); | ||
| } | ||
| @PluginMethod | ||
| public void get(PluginCall call) { | ||
| JSObject ret = new JSObject(); | ||
| String userAgent = settings.getUserAgentString(); | ||
| ret.put("userAgent", userAgent); | ||
| call.resolve(ret); | ||
| } | ||
| @PluginMethod(returnType = PluginMethod.RETURN_NONE) | ||
| public void set(PluginCall call) { | ||
| String ua = call.getString("userAgent", null); | ||
| settings.setUserAgentString(ua); | ||
| call.resolve(); | ||
| } | ||
| @PluginMethod(returnType = PluginMethod.RETURN_NONE) | ||
| public void reset(PluginCall call) { | ||
| settings.setUserAgentString(null); | ||
| call.resolve(); | ||
| } | ||
| } |
19014
2.01%145
-18.54%