chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "knockknock");
port.postMessage({joke: "Knock knock"});
});
我想使用' port'在此Chrome API函数之外,我该怎么做?
答案 0 :(得分:1)
简单地:
在响应之后调用一个函数将值传递给它:
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "knockknock");
port.postMessage({joke: "Knock knock"});
callback(port);
});
function callback(value){
console.log(value); //accessed value outside that function
}
OR
创建一个全局变量并为其分配响应
var portValue;
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "knockknock");
port.postMessage({joke: "Knock knock"});
portValue = port;
});
取决于您希望如何使用该值,您可以调整任何方法。
答案 1 :(得分:0)
这会解决您的问题吗?
Observable.just(2, 4, 6)
.filter(value -> ((value % 2) != 0))
// replace the empty observable with an empty observable
// but this observable will log when it will completed
.switchIfEmpty(Observable.<Integer>empty().doOnTerminate(() -> System.out.println("empty !")))
.subscribe();
答案 2 :(得分:0)
someFunction function(){
return chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "knockknock");
port.postMessage({joke: "Knock knock"});
return port;
});
var port = someFunction();