mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-17 14:59:12 +02:00
Rewrite examples to use browser.* and promises (#138)
This commit is contained in:
@@ -1,26 +1,30 @@
|
||||
/* Retrieve any previously set cookie and send to content script */
|
||||
|
||||
chrome.tabs.onUpdated.addListener(cookieUpdate);
|
||||
browser.tabs.onUpdated.addListener(cookieUpdate);
|
||||
|
||||
function getActiveTab() {
|
||||
return browser.tabs.query({active: true, currentWindow: true});
|
||||
}
|
||||
|
||||
function cookieUpdate(tabId, changeInfo, tab) {
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
getActiveTab().then((tabs) => {
|
||||
/* inject content script into current tab */
|
||||
|
||||
chrome.tabs.executeScript(null, {
|
||||
browser.tabs.executeScript(null, {
|
||||
file: "/content_scripts/updatebg.js"
|
||||
});
|
||||
|
||||
// get any previously set cookie for the current tab
|
||||
|
||||
chrome.cookies.get({
|
||||
var gettingCookies = browser.cookies.get({
|
||||
url: tabs[0].url,
|
||||
name: "bgpicker"
|
||||
}, function(cookie) {
|
||||
});
|
||||
gettingCookies.then((cookie) => {
|
||||
if(cookie) {
|
||||
var cookieVal = JSON.parse(cookie.value);
|
||||
chrome.tabs.sendMessage(tabs[0].id, {image: cookieVal.image});
|
||||
chrome.tabs.sendMessage(tabs[0].id, {color: cookieVal.color});
|
||||
browser.tabs.sendMessage(tabs[0].id, {image: cookieVal.image});
|
||||
browser.tabs.sendMessage(tabs[0].id, {color: cookieVal.color});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user