{"id":18727481,"url":"https://github.com/pengwei1024/jsbridge","last_synced_at":"2025-10-26T10:12:24.388Z","repository":{"id":63924979,"uuid":"41778292","full_name":"pengwei1024/JsBridge","owner":"pengwei1024","description":"A simpler, extendable bidirectional communication Frame between Android WebView and Javascript","archived":false,"fork":false,"pushed_at":"2019-11-18T12:33:03.000Z","size":700,"stargazers_count":644,"open_issues_count":13,"forks_count":89,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-03-29T05:08:15.365Z","etag":null,"topics":["addjavascriptinterface","android","javascriptbridge","jsbridge","jscallback-call","webview","webviewjavascriptbridge"],"latest_commit_sha":null,"homepage":"https://github.com/pengwei1024/JsBridge/wiki","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pengwei1024.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.txt","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-02T03:46:37.000Z","updated_at":"2025-03-18T10:45:31.000Z","dependencies_parsed_at":"2023-01-14T14:30:47.587Z","dependency_job_id":null,"html_url":"https://github.com/pengwei1024/JsBridge","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengwei1024%2FJsBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengwei1024%2FJsBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengwei1024%2FJsBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengwei1024%2FJsBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pengwei1024","download_url":"https://codeload.github.com/pengwei1024/JsBridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247294539,"owners_count":20915340,"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":["addjavascriptinterface","android","javascriptbridge","jsbridge","jscallback-call","webview","webviewjavascriptbridge"],"created_at":"2024-11-07T14:17:49.366Z","updated_at":"2025-10-26T10:12:19.342Z","avatar_url":"https://github.com/pengwei1024.png","language":"Java","readme":"English | [简体中文](./README_CN.md)\n\n# JsBridge\nA simpler, extendable bidirectional communication Frame between Android WebView and Javascript\n\n## Features\n- supports parsing and callback for JS primitive types\n- Modular management\n- support System WebView and Custom WebView\n- permission authentication is implemented by Native, JS do not need to depend other file\n- support Android API 8+, avoid addJavascriptInterface Vulnerability\n- compatible with iOS [WebViewJavascriptBridge](https://github.com/pengwei1024/JsBridge-WebViewJavascriptBridge-Sample)\n\n## Getting Started\nDownload [the latest JAR](./jars) or Gradle:\n\n```\ncompile 'com.apkfuns.jsbridge:jsbridge:2.1.1'\n```\nThe library dependen on `support-annotations`, if your project already exists, please exclude\n\n```\ncompile('com.apkfuns.jsbridge:jsbridge:2.1.1') {\n    exclude module: 'support-annotations'\n}\n```\n\n## Examples\nWe use JS to call the original module to achieve `ajax cross-domain request` to briefly introduce the use of the library\n\n#### 1.Create Module\nCreate a module that needs to inherit `JsModule` and implement the` getModuleName `method, the module naming request is the same as the java variable naming, must not be empty, only allow `underline(_)` `Letters` 和`Number`, if is `static Module` (not contain module name)，need to inherit `JsStaticModule`, the following creates a Native module\n\n ```java\n public class NativeModule extends JsModule {\n     @Override\n     public String getModuleName() {\n         return \"native\";\n     }\n }\n ```\n \n#### 2.Create Native Method\nModule inside the creation method requires the use of annotations `@ JSBridgeMethod`, by default Java method name is JS call method name, also specify the name of the calling method by `@JSBridgeMethod (methodName = \"xx\")`。Method can not be `static` or` abstract`, method can contain the return type, if the return type is the object, default return string to JS, the method parameters for the following types, you can directly map to their corresponding JS type\n\nJava Types | Mapping the JS type\n----|------\nBoolean / boolean | Bool  \nInteger/ int | Number\nFloat / float | Number\nDouble / double | Number\nLong / long | Number\nString | String\nJBCallback | function\nJBMap | Object\nJBArray | Array\n\nfor more convenient to call, we define the method parameters as ajax, we look at the ajax request structure\n\n```javascript\n$.ajax({\n    type:'GET',\n    url:'xxx.com',\n    dataType:'text'\n    data:{a:1, b:'xx'},\n    success:function(data){\n    },\n    error:function(err){\n    }\n})\n```\nAjax method parameter is a JS object, the object contains type, url, dataType three string parameters, data parameter is an object, success and error is JS callback method, let's define the Java method.\n\n```java\n@JSBridgeMethod\npublic void ajax(JBMap dataMap) {\n        String type = dataMap.getString(\"type\");\n        String url = dataMap.getString(\"url\");\n        JBMap data = dataMap.getJBMap(\"data\");\n        JBCallback successCallback = dataMap.getCallback(\"success\");\n        JBCallback errorCallback = dataMap.getCallback(\"error\");\n        // Omit the request code\n        if (request success) {\n              successCallback.apply(\"success\");\n        } else {\n              errorCallback.apply(\"failure\");\n        }\n}\n```\n must add annotations `@JSBridgeMethod`，the parameter is JBMap\n \n `JBCallback.apply` callback JS callback method，variable parameter，support for Java basic types，Array (WritableJBArray)，and Object(WritableJBMap), if for other objects, the default converted to string\n     \n#### 3.Register Module\nThere are two ways to register a Module, `Default registration`, `Dynamic registration`\n\n ```java\nJsBridgeConfig.getSetting().registerDefaultModule(NativeModule.class);\n\n// or\n\nJsBridge.loadModule(NativeModule.class)\n ```  \n JsBridgeConfig parameter:\n \nMethod | Type | Description | Default\n----|------ |------|------\nsetProtocol|string|The name of the object that JS calls| JsBridge\nsetLoadReadyMethod|string|Load the completion of the callback function| onJsBridgeReady\nregisterDefaultModule|JsModule|Common module, default load| None\ndebugMode| bool | In debug mode, the output TAG is the JsBridgeDebug log| false \n \n#### 4.WebView inject method \u0026 listen callback\n ```java\n public class WebViewActivity extends BaseActivity {\n    private JsBridge jsBridge;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n          ...\n        jsBridge = JsBridge.loadModule();\n        ...\n        webView.setWebChromeClient(new WebChromeClient() {\n            @Override\n            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {\n                // listen callback\n                jsBridge.callJsPrompt(message, result);\n                return true;\n            }\n        });\n\n        webView.setWebViewClient(new WebViewClient() {\n            @Override\n            public void onPageFinished(WebView view, String url) {\n                super.onPageFinished(view, url);\n                // inject JS\n                jsBridge.injectJs(view);\n            }\n        });\n    }\n\n    @Override\n    protected void onDestroy() {\n        // Avoid memory leaks\n        jsBridge.release();\n        super.onDestroy();\n    }\n}\n\n ```\n Now, in JS code can call this method:\n \n```javascript\nJsBridge.native.ajax({\n    type:'GET',\n    url:'xxx.com',\n    dataType:'text'\n    data:{a:1, b:'xx'},\n    success:function(data){\n    },\n    error:function(err){\n    }\n})\n```\nIf it is like calling `JsBridge.ajax({...})`, change the parent class from `JsModule` to `JsStaticModule`\n\nThere are some important information, because JS execution is asynchronous, in order to ensure that injection JS has been completed, please implement the method in the callback, or judge JsBridge object exists\n\n```javascript\nwindow.onJsBridgeReady = function () {\n    JsBridge.native.ajax({...});\n}\n\n// or\ndocument.addEventListener('onJsBridgeReady', function(){\n    JsBridge.native.ajax({...});\n})\n\n// or\nif (JsBridge) {\n    JsBridge.native.ajax({...});\n}\n\n```\n\nFor documentation and additional information see [wiki](https://github.com/pengwei1024/JsBridge/wiki) and [sample](./sample)\n\n## Proguard\n```\n-keep class com.apkfuns.jsbridge.**{*;}\n-keep class * extends com.apkfuns.jsbridge.module.JsModule{*;}\n```\n\n## License\n\u003cpre\u003e\nCopyright pengwei1024\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\u003c/pre\u003e","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpengwei1024%2Fjsbridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpengwei1024%2Fjsbridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpengwei1024%2Fjsbridge/lists"}