Rewrite examples to use browser.* and promises (#138)

This commit is contained in:
wbamberg
2016-11-15 10:12:55 -08:00
committed by GitHub
parent 664239dac0
commit c5d69d15d6
33 changed files with 290 additions and 244 deletions

View File

@@ -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});
}
});
});
}
}

View File

@@ -1,7 +1,7 @@
var html = document.querySelector('html');
var body = document.querySelector('body');
chrome.runtime.onMessage.addListener(updateBg);
browser.runtime.onMessage.addListener(updateBg);
function updateBg(request, sender, sendResponse) {
if(request.image) {
@@ -16,4 +16,4 @@ function updateBg(request, sender, sendResponse) {
body.style.backgroundImage = '';
body.style.backgroundColor = '';
}
}
}

View File

@@ -6,6 +6,10 @@ var reset = document.querySelector('.color-reset button');
var cookieVal = { image : '',
color : '' };
function getActiveTab() {
return browser.tabs.query({active: true, currentWindow: true});
}
/* apply backgrounds to buttons */
/* add listener so that when clicked, button applies background to page HTML */
@@ -15,13 +19,13 @@ for(var i = 0; i < bgBtns.length; i++) {
bgBtns[i].style.backgroundImage = bgImg;
bgBtns[i].onclick = function(e) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
getActiveTab().then((tabs) => {
var imgName = e.target.getAttribute('class');
var fullURL = chrome.extension.getURL('popup/images/'+ imgName + '.png');
chrome.tabs.sendMessage(tabs[0].id, {image: fullURL});
var fullURL = browser.extension.getURL('popup/images/'+ imgName + '.png');
browser.tabs.sendMessage(tabs[0].id, {image: fullURL});
cookieVal.image = fullURL;
chrome.cookies.set({
browser.cookies.set({
url: tabs[0].url,
name: "bgpicker",
value: JSON.stringify(cookieVal)
@@ -33,12 +37,12 @@ for(var i = 0; i < bgBtns.length; i++) {
/* apply chosen color to HTML background */
colorPick.onchange = function(e) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
getActiveTab().then((tabs) => {
var currColor = e.target.value;
chrome.tabs.sendMessage(tabs[0].id, {color: currColor});
browser.tabs.sendMessage(tabs[0].id, {color: currColor});
cookieVal.color = currColor;
chrome.cookies.set({
browser.cookies.set({
url: tabs[0].url,
name: "bgpicker",
value: JSON.stringify(cookieVal)
@@ -49,12 +53,12 @@ colorPick.onchange = function(e) {
/* reset background */
reset.onclick = function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {reset: true});
getActiveTab().then((tabs) => {
browser.tabs.sendMessage(tabs[0].id, {reset: true});
cookieVal = { image : '',
color : '' };
chrome.cookies.remove({
browser.cookies.remove({
url: tabs[0].url,
name: "bgpicker"
})
@@ -63,6 +67,9 @@ reset.onclick = function() {
/* Report cookie changes to the console */
chrome.cookies.onChanged.addListener(function(changeInfo) {
console.log('Cookie changed:\n* Cookie: ' + JSON.stringify(changeInfo.cookie) + '\n* Cause: ' + changeInfo.cause + '\n* Removed: ' + changeInfo.removed);
})
browser.cookies.onChanged.addListener((changeInfo) => {
console.log(`Cookie changed:\n
* Cookie: ${JSON.stringify(changeInfo.cookie)}\n
* Cause: ${changeInfo.cause}\n
* Removed: ${changeInfo.removed}`);
});