|
@@ -65,10 +65,38 @@ func createJSContext() -> JSContext {
|
|
return ctx!
|
|
return ctx!
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+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)])
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func testSearch(keyword: String, ctx: JSContext) -> Void {
|
|
func testSearch(keyword: String, ctx: JSContext) -> Void {
|
|
if let searchFunction = ctx.objectForKeyedSubscript("search") {
|
|
if let searchFunction = ctx.objectForKeyedSubscript("search") {
|
|
print(searchFunction)
|
|
print(searchFunction)
|
|
- let result = searchFunction.call(withArguments: ["周杰伦", nil])
|
|
|
|
|
|
+ let result = searchFunction.call(withArguments: [keyword, nil])
|
|
print(result?.toString())
|
|
print(result?.toString())
|
|
let resolveHandler: @convention(block) (JSValue?) -> Void = { resolvedValue in
|
|
let resolveHandler: @convention(block) (JSValue?) -> Void = { resolvedValue in
|
|
if let value = resolvedValue {
|
|
if let value = resolvedValue {
|
|
@@ -101,7 +129,8 @@ if let url = URL(string: "http://hubgit.cn/ben/be-ytb/raw/master/js/info.js") {
|
|
case .success(let jsString):
|
|
case .success(let jsString):
|
|
print("下载远程JS成功")
|
|
print("下载远程JS成功")
|
|
ctx.evaluateScript(jsString)
|
|
ctx.evaluateScript(jsString)
|
|
- testSearch(keyword: "周杰伦", ctx: ctx)
|
|
|
|
|
|
+ testDetail(url: "https://www.youtube.com/watch?v=7wNb0pHyGuI", ctx: ctx)
|
|
|
|
+// testSearch(keyword: "周杰伦", ctx: ctx)
|
|
case .failure(let error):
|
|
case .failure(let error):
|
|
print("Download Error: \(error)")
|
|
print("Download Error: \(error)")
|
|
}
|
|
}
|