{"id":20690500,"url":"https://github.com/octree/octwebviewbridge","last_synced_at":"2025-07-27T00:04:00.907Z","repository":{"id":56921146,"uuid":"85790598","full_name":"octree/OCTWebViewBridge","owner":"octree","description":"WKWebView Javascript  Bridge","archived":false,"fork":false,"pushed_at":"2019-02-12T06:56:03.000Z","size":115,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-18T01:40:46.854Z","etag":null,"topics":["bridge","ios","javscript","webview","wkwebview"],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/octree.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}},"created_at":"2017-03-22T05:58:56.000Z","updated_at":"2019-02-13T07:32:38.000Z","dependencies_parsed_at":"2022-08-20T21:50:28.366Z","dependency_job_id":null,"html_url":"https://github.com/octree/OCTWebViewBridge","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FOCTWebViewBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FOCTWebViewBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FOCTWebViewBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/octree%2FOCTWebViewBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/octree","download_url":"https://codeload.github.com/octree/OCTWebViewBridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250284503,"owners_count":21405294,"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":["bridge","ios","javscript","webview","wkwebview"],"created_at":"2024-11-16T23:13:26.074Z","updated_at":"2025-04-22T16:57:56.511Z","avatar_url":"https://github.com/octree.png","language":"Objective-C","readme":"![img](https://img.shields.io/github/license/octree/OCTWebViewBridge.svg)\n![img](https://img.shields.io/cocoapods/v/OCTWebViewBridge.svg?style=flat)\n\n## OCTWebViewBridge\n\n起源于在老东家工作的时候的库 [YJWebView](https://github.com/TinydustDevelopers/YJWebView)\n\n\n在 WKWebView 中使用 `Javascript` 调用 Native 代码，支持函数作为参数。\n\n```javascript\nwindow.native.foo(param1, param2, (foo, bar) =\u003e {\n\n}, (error) =\u003e {  })\n```\n\n## Installation\n\n```shell\npod 'OCTWebViewBridge'\n```\n\n\n\n## How To Use\n\n### Custom Plugin\n\njavascript file（log.js）:\n```javascript\nwindow.bridge.log = function(msg) {\n\nwindow.bridge.invoke(\"me.octree.bridge.log\", \"log:\", msg)\n}\n```\n\nobjective-c code\n\n```objectivec\n@interface OCTLogPlugin : NSObject \u003cOCTWebViewPlugin\u003e\n\n@property (copy, nonatomic, readonly) NSString *identifier;\n@property (copy, nonatomic, readonly) NSString *javascriptCode;\n\n@end\n\n\n@implementation OCTLogPlugin\n\n- (NSString *)identifier {\n    \n    return @\"me.octree.bridge.log\";\n}\n\n- (NSString *)javascriptCode {\n    \n    NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@\"log\" ofType:@\"js\"];\n    return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];\n}\n\n\n- (void)log:(id)msg {\n\n    NSLog(@\"WebView Bridge: %@\", [msg description]);\n}\n\n@end\n\n```\n\ninject plugin \n\n```objectivec\n[[OCTWebViewPluginInjector injectorForWebView:webView] injectPlugin:[[OCTLogPlugin alloc] init]];\n```\n\njavascript 调用 Native 代码\n\n```\nwindow.bridge.log(\"yoooooooo man\")\n```\n\n### Block Plugin\n\n使用 Block 的方式为 Web 提供插件。\nWeb 调用时，会执行这个 Block\n\n```objectivec\n[[OCTWebViewPluginInjector injectorForWebView:_webView] injectPluginWithFunctionName:@\"test\" handler:^(NSDictionary *data) { \n    NSLog(@\"%@\", data);\n}];\n\n[[OCTWebViewPluginInjector injectorForWebView:_webView] injectPluginWithFunctionName:@\"test2\" handlerWithResponseBlock:^(NSDictionary *data, OCTResponseCallback responseCallback) {\n   NSLog(@\"test2: %@\", data);\n   responseCallback(@{ @\"hello\" : @\"world\" });\n}];\n```\n\n在 Web 中调用\n\n```javascript\nwindow.bridge.plugin.test({'hello': 'world'})\nwindow.bridge.plugin.test2({'hello': 'world'}, function(json) {\n  window.bridge.log(JSON.stringify(json))\n})\n```\n\n\n\n### CSS Injector\n\n注入 CSS 代码\n\n```objectivec\n// inject css\n[[OCTWebViewPluginInjector injectorForWebView:_webView] injectCSSString:@\"body {background-color: #eeeeee;}\" forIdentifier:@\"test\"];\n// remove css\n[[OCTWebViewPluginInjector injectorForWebView:_webView] removeCSSStringForIdentifier:@\"test\"];\n```\n\n### Night Mode \n\n开启网页夜间模式\n\n```objectivec\n\n// enable night mode\n[self.webView oct_nightFall];\n\n// disable night mode\n[self.webView oct_sunrise];\n\n```\n\n## License\n\nOCTWebViewBridge is released under the MIT license. See LICENSE for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctree%2Foctwebviewbridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foctree%2Foctwebviewbridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foctree%2Foctwebviewbridge/lists"}