javascript - How can I add a callback function to chrome extension api? -
actually, want use webrequest modify cookie before request sending. own cookie data stored in extension's storage, getting data storage asynchronous. how can modify request's cookie?
i want this:
var beforesendheadershandler = { func: function(details, addcookie){ addcookie(details) return {requestheaders: details.requestheaders} }, filter: { urls: ["<all_urls>"] }, extra: ["blocking", "requestheaders"] }; chrome.webrequest.onbeforesendheaders.addlistener(beforesendheadershandler.func, beforesendheadershandler.filter, beforesendheadershandler.extra);
how can add callback argument chrome.webrequest.onbeforesendheaders.addlistener callback function, or other solution?
if want that, use promise
var beforesendheadershandler = { func: function(details, addcookie){ var promise = new promise(function(resolve) { chrome.storage.local.get("your key", function(results) { resolve(results["your key"]); }); }; promise.then(function(value) { return {requestheaders: value}; }); }, filter: { urls: ["<all_urls>"] }, extra: ["blocking", "requestheaders"] }; chrome.webrequest.onbeforesendheaders.addlistener(beforesendheadershandler.func, beforesendheadershandler.filter, beforesendheadershandler.extra);
recommended
see synchronous call in google chrome extension , asynchronous operation in blocking chrome.webrequest.onbeforesendheaders listener, it's recommended if make copy outside of listener, or if possible, save storage using localstorage, synchronous.
Comments
Post a Comment