Chrome app executeScript with setTimeout, 回调不等待
Chrome app executeScript with setTimeout, callback not waiting
我正在做一个 Chrome 应用程序,但我遇到了一个问题:
我首先在 <webview>
打开的网站中注入 file.js
。
在这个 file.js
中,我有一个函数执行 return 值包含在 setTimeout
.
问题是当我使用 executeScript
调用函数时,回调不会等待 setTimeout
和 returns null
的结束。
有人能告诉我该怎么做吗?谢谢!
(没有setTimeout
,它return是期望值)
//content of file.js
function foo(){
setTimeout(function (){ return {key:"value"}; }, 5000);
}
//I inject the file.js in the website
webview.executeScript({file:"file.js"}, function(result){ console.log(result); });
//I call foo() in the website
webview.executeScript( {code:"foo();"}, function(result){ console.log(JSON.stringify(result)); });
executeScript
的回调将同步接收您 运行 脚本的最后评估值。
您不能等待异步事件(setTimeout
是)。
你最好的选择是用 chrome.runtime.sendMessage
发回数据。
我正在做一个 Chrome 应用程序,但我遇到了一个问题:
我首先在 <webview>
打开的网站中注入 file.js
。
在这个 file.js
中,我有一个函数执行 return 值包含在 setTimeout
.
问题是当我使用 executeScript
调用函数时,回调不会等待 setTimeout
和 returns null
的结束。
有人能告诉我该怎么做吗?谢谢!
(没有setTimeout
,它return是期望值)
//content of file.js
function foo(){
setTimeout(function (){ return {key:"value"}; }, 5000);
}
//I inject the file.js in the website
webview.executeScript({file:"file.js"}, function(result){ console.log(result); });
//I call foo() in the website
webview.executeScript( {code:"foo();"}, function(result){ console.log(JSON.stringify(result)); });
executeScript
的回调将同步接收您 运行 脚本的最后评估值。
您不能等待异步事件(setTimeout
是)。
你最好的选择是用 chrome.runtime.sendMessage
发回数据。