{"id":15648275,"url":"https://github.com/yale8848/easyjsbridge","last_synced_at":"2025-07-31T06:09:18.925Z","repository":{"id":92882580,"uuid":"130466453","full_name":"yale8848/EasyJSBridge","owner":"yale8848","description":"让JS在Android/iOS WebView中反调接口统一，调用更容易","archived":false,"fork":false,"pushed_at":"2018-08-23T10:08:47.000Z","size":8,"stargazers_count":43,"open_issues_count":2,"forks_count":15,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-30T14:43:06.673Z","etag":null,"topics":["android","ios","js","jsbridge","jsbridge-webview"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yale8848.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-21T11:14:45.000Z","updated_at":"2025-01-03T07:10:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"fb38dd33-ca6c-4658-97a3-1b2fb224835c","html_url":"https://github.com/yale8848/EasyJSBridge","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yale8848/EasyJSBridge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yale8848%2FEasyJSBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yale8848%2FEasyJSBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yale8848%2FEasyJSBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yale8848%2FEasyJSBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yale8848","download_url":"https://codeload.github.com/yale8848/EasyJSBridge/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yale8848%2FEasyJSBridge/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267997174,"owners_count":24178251,"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","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["android","ios","js","jsbridge","jsbridge-webview"],"created_at":"2024-10-03T12:24:23.939Z","updated_at":"2025-07-31T06:09:18.892Z","avatar_url":"https://github.com/yale8848.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasyJSBridge\n\nEasyJSBridge让JS在Android/iOS WebView中反调接口统一，调用更容易；\n\n## JS调用Demo\n\n```JavaScript\n\n\u003cscript type=\"text/javascript\" src=\"EasyJSBridge.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\u003e\n    var methods = [\"method1\", \"method2\", \"method3\"];\n    var easyJSBridge = EasyJSBridge.create(\"android\", \"ios\", methods);\n    $(\".method1\").click(function() {\n        easyJSBridge.method1(\"parameter1\",function(data1,data2){\n            console.log(data1);\n        });\n    });\n    $(\".method2\").click(function() {\n        easyJSBridge.method2(\"parameter1\")\n    });\n    $(\".method3\").click(function() {\n        easyJSBridge.method3(\"androidParameter1\", 2, [\"iosParameter1\", 2, \"3\"])\n    });\n\u003c/script\u003e\n\n\n```\n\n## 反调接口文档Demo\n\n### 方法：method1(parameter1,function(data){})  \n    参数：parameter1 string\n    回调方法：function(data1,data2){}\n\n### 方法：method2(parameter1,parameter2) \n    参数：parameter1 string\n    参数：parameter2 int\n\n### 方法：[android] method3(parameter1,parameter2)  \n    参数：parameter1 string\n    参数：parameter2 int\n    \n    [iOS] method3(parameter1,parameter2,parameter3)\n    \n    参数：parameter1 string\n    参数：parameter2 int\n    参数：parameter3 string\n\n\n`通过反调接口文档Demo的函数定义，JS调用Demo中无论是android还是iOS，反调函数名称都是一样的，同时兼容android和iOS反调参数不一致的情况`\n\n## 约定\n\n- 在Android中通过`webView.addJavascriptInterface(obj,'android')` 绑定反调；\n- 在iOS中通过`WKWebView`的`WKScriptMessageHandler`绑定反调，可以有两种方式绑定，二选一即可： \n  \n  1. 通过addScriptMessageHandler添加一个name，然后通过约定的参数来解析要调用的方法和参数\n\n        ```\n        [userContentController addScriptMessageHandler:self name:@\"iOS\"]; \n\n        ```\n\n        然后解析js调用的数据：\n\n        ```\n        window.webkit.messageHandlers.iOS.postMessage({method:'method1',parameter:['','']});\n        window.webkit.messageHandlers.iOS.postMessage({method:'method2',parameter:['','']})\n        ```\n       注意`{method:'method1',parameter:['','']}`中的`method`和`parameter`必须约定如此,`parameter`是参数数组，iOS需自己解析参数；\n\n  2. 通过addScriptMessageHandler添加多个name，然后根据不同name来区分调用的方法\n\n       ```\n       [userContentController addScriptMessageHandler:self name:@\"method1\"];\n       [userContentController addScriptMessageHandler:self name:@\"method2\"]; \n\n       ```\n\n       ```\n   \n       if ([message.name isEqualToString:@\"method1\"]) {\n  \n    \n       } else if ([message.name isEqualToString:@\"method2\"]) {\n     \n       }\n       ```\n   \n       然后解析js调用的数据：\n\n       ```\n       window.webkit.messageHandlers.method1.postMessage('arg1',2)\n       window.webkit.messageHandlers.method2.postMessage('arg1','arg2')\n       ```\n- JS调用中，约定的反调方法名称都要显式在数组中初始化\n\n  ```\n    var methods = [\"method1\", \"method2\", \"method3\"];\n    var easyJSBridge = EasyJSBridge.create(\"android\", \"ios\", methods);\n  ```\n\n- 回调\n  \n  Android,iOS 回调js通过调用Js函数  `EasyJSBridge.method(\"parmaters\");` 来实现回调\n\n  比如上面demo中的method1最后一个匿名函数就是回调函数，Android或者iOS通过调用 `EasyJSBridge.method1(\"parmaters\",\"parmaters2\");` 即可，Android WebView注意要放在主线程调用；\n\n  ```\n   easyJSBridge.method1(\"parameter1\",function(data1,data2){//回调函数\n            console.log(data1);\n        });\n  ```\n\n## API\n\n### 初始化\n\nvar easyJSBridge = EasyJSBridge.create('androidObj','iOSObj',[methodList])\n\n|参数名|必选|类型|说明|\n|:----    |:---|:----- |-----   |\n|androidObj |是  |string |Android WebView绑定的对象名称  |\n|iOSObj |否  |string | iOS WebView绑定的对象名称    |\n|methodList     |是  |数组 | 方法名称列表    |\n\n**备注** \n\n- 如果有iOSObj，那就按照如下方法约定反调\n  \n ```\n    window.webkit.messageHandlers.iOS.postMessage({method:'method1',parameter:['','']});\n    window.webkit.messageHandlers.iOS.postMessage({method:'method2',parameter:['','']})\n ```\n\n- 如果没有iOSObj，那就按照如下方法约定反调\n\n ```\n    window.webkit.messageHandlers.method1.postMessage('arg1',2)\n    window.webkit.messageHandlers.method2.postMessage('arg1','arg2')\n ```\n\n### 调用反调\n\n先初始化\n\n```\nvar methodList = ['method1','method2'];\nvar easyJSBridge = EasyJSBridge.create('androidObj','iOSObj',methodList);\n\n```\n\n然后根据初始化时的methodList方法名直接调用\n\n```\neasyJSBridge.method1(\"arg1\",\"arg2\");\neasyJSBridge.method2(\"arg1\",\"arg2\");\n```\n\n**备注** \n\n1. 如果Android和iOS调用参数值不一样，或者参数不一样，可以如下调用：\n\n```\neasyJSBridge.method1(\"androidArg1\",\"androidArg2\",[\"iOSArg1\",\"iOSArg2\",\"iOSArg3\"]);\n```\nmethod1中的数组参数给iOS用，前面的参数给android用\n\n2. 页面在PC浏览器打开反调不会报错，可以放心使用；\n\n## License\n\n```\nMIT License\n\nCopyright (c) 2018 Yale\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyale8848%2Feasyjsbridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyale8848%2Feasyjsbridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyale8848%2Feasyjsbridge/lists"}