Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add execute script #100

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Then the permission will be asked when the camera is used.
* [`getCookies(...)`](#getcookies)
* [`close()`](#close)
* [`openWebView(...)`](#openwebview)
* [`executeScript(...)`](#executescript)
* [`setUrl(...)`](#seturl)
* [`addListener('urlChangeEvent', ...)`](#addlistenerurlchangeevent)
* [`addListener('closeEvent', ...)`](#addlistenercloseevent)
Expand Down Expand Up @@ -138,6 +139,21 @@ Open url in a new webview with toolbars
--------------------


### executeScript(...)

```typescript
executeScript({ code }: { code: string; }) => Promise<void>
```

Injects JavaScript code into the InAppBrowser window.

| Param | Type |
| --------- | ------------------------------ |
| **`__0`** | <code>{ code: string; }</code> |

--------------------


### setUrl(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ public void run() {
);
}

@PluginMethod
public void executeScript(PluginCall call) {
String script = call.getString("code");
if (script == null || TextUtils.isEmpty(script)) {
call.reject("No script to run");
}

if (webViewDialog != null) {
this.getActivity()
.runOnUiThread(
new Runnable() {
@Override
public void run() {
webViewDialog.executeScript(script);
}
}
);
}

call.resolve();
}

@PluginMethod
public void reload(PluginCall call) {
if (webViewDialog != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public String getUrl() {
return _webView.getUrl();
}

public void executeScript(String script) {
_webView.evaluateJavascript(script, null);
}

public void setUrl(String url) {
Map<String, String> requestHeaders = new HashMap<>();
if (_options.getHeaders() != null) {
Expand Down
8 changes: 8 additions & 0 deletions ios/Plugin/InAppBrowserPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ public class InAppBrowserPlugin: CAPPlugin {
call.resolve()
}

@objc func executeScript(_ call: CAPPluginCall) {
guard let script = call.getString("code") else {
call.reject("Cannot get script to execute")
return
}
self.webViewController?.executeScript(script: script)
}

func isHexColorCode(_ input: String) -> Bool {
let hexColorRegex = "^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$"

Expand Down
6 changes: 4 additions & 2 deletions ios/Plugin/WKWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ open class WKWebViewController: UIViewController {
self.previousToolbarState = (navigation.toolbar.tintColor, navigation.toolbar.isHidden)
}

// self.restateViewHeight()

if let s = self.source {
self.load(source: s)
} else {
Expand Down Expand Up @@ -380,6 +378,10 @@ public extension WKWebViewController {
func reload() {
webView?.reload()
}

func executeScript(script: String, completion: ((Any?, Error?) -> Void)? = nil) {
webView?.evaluateJavaScript(script, completionHandler: completion)
}
}

// MARK: - Fileprivate Methods
Expand Down
4 changes: 4 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ export interface InAppBrowserPlugin {
* @since 0.1.0
*/
openWebView(options: OpenWebViewOptions): Promise<any>;
/**
* Injects JavaScript code into the InAppBrowser window.
*/
executeScript({ code }: { code: string }): Promise<void>;
setUrl(options: { url: string }): Promise<any>;
/**
* Listen for url change, only for openWebView
Expand Down
5 changes: 5 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class InAppBrowserWeb extends WebPlugin implements InAppBrowserPlugin {
return options;
}

async executeScript({ code }: {code: string }): Promise<any> {
console.log("code", code);
return code;
}

async close(): Promise<any> {
console.log("close");
return;
Expand Down
Loading