Ben 11 kuukautta sitten
vanhempi
commit
310e7ad836
1 muutettua tiedostoa jossa 11 lisäystä ja 28 poistoa
  1. 11 28
      js/main.swift

+ 11 - 28
js/main.swift

@@ -68,40 +68,23 @@ func createJSContext() -> JSContext {
 
 func testDetail(url: String, ctx: JSContext) -> Void {
     if let detailFunction = ctx.objectForKeyedSubscript("detail") {
-        print(detailFunction)
         let result = detailFunction.call(withArguments: [url])
-        print(result?.toString())
-        let resolveHandler: @convention(block) (JSValue?) -> Void = { resolvedValue in
-            if let value = resolvedValue {
-                print("解析成功: ", value)
-            }
-        }
-        let rejectHandler: @convention(block) (JSValue?) -> Void = { rejectedValue in
-            if let value = rejectedValue {
-                print("解析失败:", value)
-            }
-        }
-        if let thenFunction = result?.objectForKeyedSubscript("then") {
-            print("Then:", thenFunction.toString())
-            // 调用 then 函数,并传入回调函数
-            thenFunction.call(withArguments: [unsafeBitCast(resolveHandler, to: AnyObject.self)])
-        }
-        if let catchFunction = result?.objectForKeyedSubscript("catch") {
-            print("Catch:", catchFunction.toString())
-            // 调用 catch 函数,并传入回调函数
-            catchFunction.call(withArguments: [unsafeBitCast(rejectHandler, to: AnyObject.self)])
+        let completionHandler: @convention(block) (JSValue) -> Void = { result in
+            print("详情结果!!: \(result.toDictionary())")
         }
+        let completionFunction = unsafeBitCast(completionHandler, to: AnyObject.self)
+        result?.invokeMethod("then", withArguments: [completionFunction])
     }
 }
 
 func testSearch(keyword: String, ctx: JSContext) -> Void {
     if let search = ctx.objectForKeyedSubscript("search") {
-        let promise = search.call(withArguments: [keyword])
-        let thenCallback: @convention(block) (String) -> Void = { result in
-            print("搜索结果: ", result)
+        let result = search.call(withArguments: [keyword])
+        let completionHandler: @convention(block) (JSValue) -> Void = { result in
+            print("搜索结果!!: \(result.toDictionary())")
         }
-        let then = promise?.objectForKeyedSubscript("then")
-        then?.call(withArguments: [thenCallback])
+        let completionFunction = unsafeBitCast(completionHandler, to: AnyObject.self)
+        result?.invokeMethod("then", withArguments: [completionFunction])
     }
 }
 
@@ -113,8 +96,8 @@ if let url = URL(string: "http://hubgit.cn/ben/be-ytb/raw/master/js/info.js") {
         case .success(let jsString):
             print("下载远程JS成功")
             ctx.evaluateScript(jsString)
-//            testDetail(url: "https://www.youtube.com/watch?v=7wNb0pHyGuI", ctx: ctx)
-            testSearch(keyword: "周杰伦", ctx: ctx)
+            testDetail(url: "https://www.youtube.com/watch?v=d0R-JyU4Btk", ctx: ctx)
+//            testSearch(keyword: "周杰伦", ctx: ctx)
         case .failure(let error):
             print("Download Error: \(error)")
         }