{"id":24918014,"url":"https://github.com/douking/stmscriptmessagehandler","last_synced_at":"2026-04-28T20:32:38.704Z","repository":{"id":53164523,"uuid":"74754443","full_name":"DouKing/STMScriptMessageHandler","owner":"DouKing","description":"A script message handler for wkwebview is used to communicate with js each other.","archived":false,"fork":false,"pushed_at":"2023-06-27T06:26:23.000Z","size":9207,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-22T17:04:30.851Z","etag":null,"topics":["javascript","js","scriptmessage","webview","wkscriptmessagehandler","wkwebview","wkwebview-javascript-bridge","wkwebview-js"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DouKing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-11-25T11:51:43.000Z","updated_at":"2024-01-12T18:03:54.000Z","dependencies_parsed_at":"2023-07-14T10:47:08.922Z","dependency_job_id":null,"html_url":"https://github.com/DouKing/STMScriptMessageHandler","commit_stats":{"total_commits":48,"total_committers":4,"mean_commits":12.0,"dds":"0.27083333333333337","last_synced_commit":"24595a0473ade87ff85455351c0890af48e42c11"},"previous_names":["douking/stmwebviewcontroller"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DouKing%2FSTMScriptMessageHandler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DouKing%2FSTMScriptMessageHandler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DouKing%2FSTMScriptMessageHandler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DouKing%2FSTMScriptMessageHandler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DouKing","download_url":"https://codeload.github.com/DouKing/STMScriptMessageHandler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245994310,"owners_count":20706752,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","js","scriptmessage","webview","wkscriptmessagehandler","wkwebview","wkwebview-javascript-bridge","wkwebview-js"],"created_at":"2025-02-02T09:17:20.787Z","updated_at":"2026-02-28T00:09:28.922Z","avatar_url":"https://github.com/DouKing.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu)\n[![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE)\n\n# STMScriptMessageHandler\n\n\u003cimg src=\"./Capture.gif\" style=\"float:right\"\u003e\n\nThe `STMScriptMessageHandler` is used to comunicate with js for `WKWebView`. It implements `WKScriptMessageHandler` and `WKScriptMessageHandlerWithReply` protocol. A `STMScriptMessageHandler` corresponding a js object. When your `WKWebView` add a `STMScriptMessageHandler`, the js side add a object on the window automatically. The  handlerName of `STMScriptMessageHandler` is the js object's name.\n\n## Features\n\n- [x] Support multi message handlers\n- [x] Support Promise\n- [x] Compatible with `WebViewJavascriptBridge`\n\n## Requirements\n\niOS 8.0+\n\n## Usage\n\n#### Native side\n\n```objectivec\n\n//When the native register a STMScriptMessageHandler called `Bridge`, the js register a object called `window.Bridge`.\nSTMScriptMessageHandler *messageHandler = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@\"Bridge\" forWebView:self.webView];\n[self.webView stm_addScriptMessageHandler:messageHandler];\n\n// register a message handler named `Page`, so the js should call your method (that the message handler registered) use `window.Page.callMethod...`\nSTMScriptMessageHandler *page = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@\"Page\" forWebView:self.webView];\n[self.webView registerScriptMessageHandler:page];\n\n[page registerMethod:@\"setButtons\" handler:^(id data, STMResponseCallback responseCallback) {\n    [self setupRightBarButtonItems:data callback:responseCallback];\n}];\n\n```\n\n#### JS side\n\n```javascript\n// Use js object `window.Bridge` call native method or register method for native.\nwindow.Bridge.callHandler('nslog', data, function (data) {\n    log('JS got native `nslog` response', data);\n});\n\nwindow.Bridge.registerMethod('log', function(data, callback){\n    var message = JSON.parse(data);\n    log('Native calling js method `log`', message);\n    callback({key: 'from js', value: 'something'});\n});\n\n// Support Promise\nasync function promise(data) {\n    // window.Bridge.callHandler('nslog', data).then(\n    //     result =\u003e log('JS got native `nslog` response', result),\n    //     error =\u003e log('JS got native `nslog` error', error)\n    // )\n\n    var p = window.Bridge.callHandler('nslog', data);\n    var result = await p;\n    log('JS got native `nslog` response', result);\n}\n```\n\n#### Migrate from `WebViewJavascriptBridge`\n\nIf you register a message handler named `WebViewJavascriptBridge` at native side, the js side dones not need modify any code.\n\n```objectivec\n\n// The bridge's name is `WebViewJavascriptBridge`\nself.bridge = [self.webView stm_addScriptMessageHandlerUseName:@\"WebViewJavascriptBridge\"];\n\n```\n\n## Installation\n\nSTMScriptMessageHandler is available through [CocoaPods](https://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'STMScriptMessageHandler'\n```\n\n## License\n\nSTMScriptMessageHandler is available under the MIT license and 996ICU license. See file `LICENSE.MIT` and `LICENSE.NPL` for more info.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdouking%2Fstmscriptmessagehandler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdouking%2Fstmscriptmessagehandler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdouking%2Fstmscriptmessagehandler/lists"}