{"id":15637136,"url":"https://github.com/linusu/jsbridge","last_synced_at":"2025-04-14T16:11:30.358Z","repository":{"id":56919834,"uuid":"125508353","full_name":"LinusU/JSBridge","owner":"LinusU","description":"🔁 Bridge your JavaScript library for usage in Swift","archived":false,"fork":false,"pushed_at":"2021-10-11T12:26:40.000Z","size":83,"stargazers_count":110,"open_issues_count":3,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-24T15:25:45.816Z","etag":null,"topics":["hacktoberfest","swift","webkit"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/LinusU.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":"2018-03-16T11:36:48.000Z","updated_at":"2024-06-06T09:10:18.000Z","dependencies_parsed_at":"2022-08-21T04:20:18.019Z","dependency_job_id":null,"html_url":"https://github.com/LinusU/JSBridge","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2FJSBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2FJSBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2FJSBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinusU%2FJSBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LinusU","download_url":"https://codeload.github.com/LinusU/JSBridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232497137,"owners_count":18532699,"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":["hacktoberfest","swift","webkit"],"created_at":"2024-10-03T11:10:23.466Z","updated_at":"2025-01-04T17:26:50.418Z","avatar_url":"https://github.com/LinusU.png","language":"Swift","readme":"# JSBridge\n\nBridge your JavaScript library for usage in Swift 🚀\n\n## Installation\n\n### SwiftPM\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/LinusU/JSBridge\", from: \"1.0.0\"),\n]\n```\n\n### Carthage\n\n```text\ngithub \"LinusU/JSBridge\" ~\u003e 1.0.0\n```\n\n## Usage\n\n**foobar.js:**\n\n```js\nwindow.Foobar = {\n  add (a, b) {\n    return a + b\n  },\n  greet (name) {\n    return `Hello, ${name}!`\n  },\n  async fetch (url) {\n    const response = await fetch(url)\n    const body = await response.text()\n\n    return { status: response.status, body }\n  }\n}\n```\n\n**Foobar.swift:**\n\n```swift\nstruct FetchResponse: Decodable {\n    let status: Int\n    let body: String\n}\n\nclass Foobar {\n    static internal let bridge: JSBridge = {\n        let libraryPath = Bundle.main.path(forResource: \"foobar\", ofType: \"js\")!\n        let libraryCode = try! String(contentsOfFile: libraryPath)\n\n        return JSBridge(libraryCode: libraryCode)\n    }()\n\n    static func add(_ lhs: Int, _ rhs: Int) -\u003e Promise\u003cInt\u003e {\n        return Foobar.bridge.call(function: \"Foobar.add\", withArgs: (lhs, rhs)) as Promise\u003cInt\u003e\n    }\n\n    static func greet(name: String) -\u003e Promise\u003cString\u003e {\n        return Foobar.bridge.call(function: \"Foobar.greet\", withArg: name) as Promise\u003cString\u003e\n    }\n\n    static func fetch(url: URL) -\u003e Promise\u003cFetchResponse\u003e {\n        return Foobar.bridge.call(function: \"Foobar.fetch\", withArg: url) as Promise\u003cFetchResponse\u003e\n    }\n}\n```\n\n## API\n\n### `JSBridge(libraryCode: String)`\n\nCreate a new `JSBridge` instance, with the supplied library source code.\n\nThe `libraryCode` should be a string of JavaScript that attaches one or more functions to the `window` object. These functions can then be called using the `call` method.\n\n### `call(function: String) -\u003e Promise\u003cVoid\u003e`\n\nCall a function without any arguments, ignoring the return value. The returned promise will settle once the function have completed running.\n\n### `call\u003cResult: Decodable\u003e(function: String) -\u003e Promise\u003cResult\u003e`\n\nCall a function without any arguments. The returned promise will settle with the return value of the function.\n\n### `call\u003cA: Encodable\u003e(function: String, withArg: A) -\u003e Promise\u003cVoid\u003e`\n\nCall a function with a single argument, ignoring the return value. The returned promise will settle once the function have completed running.\n\n### `call\u003cResult: Decodable, A: Encodable\u003e(function: String, withArg: A) -\u003e Promise\u003cResult\u003e`\n\nCall a function with a single argument. The returned promise will settle with the return value of the function.\n\n### `call\u003cA: Encodable, B: Encodable, ...\u003e(function: String, withArgs: (A, B, ...)) -\u003e Promise\u003cVoid\u003e`\n\nCall a function with multiple arguments, ignoring the return value. The returned promise will settle once the function have completed running.\n\n### `call\u003cResult: Decodable, A: Encodable, B: Encodable, ...\u003e(function: String, withArgs: (A, B, ...)) -\u003e Promise\u003cResult\u003e`\n\nCall a function with multiple arguments. The returned promise will settle with the return value of the function.\n\n## iOS\n\nTo be able to use JSBridge on iOS, you need to give JSBridge a hook to your view hierarchy. Otherwise the `WKWebView` will get suspended by the OS, and your Promises will never settle.\n\nThis is accomplished by using the `setGlobalUIHook` function before instantiating any `JSBridge` instances.\n\n**App:**\n\n```swift\n// Can be called from anywhere, e.g. your AppDelegate\nJSBridge.setGlobalUIHook(window: UIApplication.shared.windows.first)\n```\n\n**App Extension:**\n\n```swift\n// From within your root view controller\nJSBridge.setGlobalUIHook(viewController: self)\n```\n\n## Hacking\n\nThe Xcode project is generated automatically from `project.yml` using [XcodeGen](https://github.com/yonaskolb/XcodeGen). It's only checked in because Carthage needs it, do not edit it manually.\n\n```sh\n$ mint run yonaskolb/xcodegen\n💾  Saved project to JSBridge.xcodeproj\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinusu%2Fjsbridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinusu%2Fjsbridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinusu%2Fjsbridge/lists"}