From a43eb72e761ac2d4b89611d58223b37352455e0f Mon Sep 17 00:00:00 2001 From: Will Bamberg Date: Fri, 28 Apr 2017 10:08:03 -0700 Subject: [PATCH 1/3] Fix for issue 197: make content script load using manifest key instead of executeScript --- cookie-bg-picker/background_scripts/background.js | 8 +------- cookie-bg-picker/manifest.json | 9 ++++++++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/cookie-bg-picker/background_scripts/background.js b/cookie-bg-picker/background_scripts/background.js index fb0640a..fc8eca9 100644 --- a/cookie-bg-picker/background_scripts/background.js +++ b/cookie-bg-picker/background_scripts/background.js @@ -8,19 +8,13 @@ function getActiveTab() { function cookieUpdate(tabId, changeInfo, tab) { getActiveTab().then((tabs) => { - /* inject content script into current tab */ - - browser.tabs.executeScript(null, { - file: "/content_scripts/updatebg.js" - }); - // get any previously set cookie for the current tab var gettingCookies = browser.cookies.get({ url: tabs[0].url, name: "bgpicker" }); gettingCookies.then((cookie) => { - if(cookie) { + if (cookie) { var cookieVal = JSON.parse(cookie.value); browser.tabs.sendMessage(tabs[0].id, {image: cookieVal.image}); browser.tabs.sendMessage(tabs[0].id, {color: cookieVal.color}); diff --git a/cookie-bg-picker/manifest.json b/cookie-bg-picker/manifest.json index 4e5c915..e626e70 100644 --- a/cookie-bg-picker/manifest.json +++ b/cookie-bg-picker/manifest.json @@ -36,5 +36,12 @@ "background": { "scripts": ["background_scripts/background.js"] - } + }, + + "content_scripts": [ + { + "matches": [""], + "js": ["content_scripts/updatebg.js"] + } + ] } From 06cac5f7a677bf7d9bc4f92811203d52dc3c6313 Mon Sep 17 00:00:00 2001 From: Will Bamberg Date: Fri, 28 Apr 2017 11:07:37 -0700 Subject: [PATCH 2/3] Update on tab activate as well as update --- cookie-bg-picker/background_scripts/background.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cookie-bg-picker/background_scripts/background.js b/cookie-bg-picker/background_scripts/background.js index fc8eca9..9c93b3c 100644 --- a/cookie-bg-picker/background_scripts/background.js +++ b/cookie-bg-picker/background_scripts/background.js @@ -1,12 +1,10 @@ /* Retrieve any previously set cookie and send to content script */ -browser.tabs.onUpdated.addListener(cookieUpdate); - function getActiveTab() { return browser.tabs.query({active: true, currentWindow: true}); } -function cookieUpdate(tabId, changeInfo, tab) { +function cookieUpdate() { getActiveTab().then((tabs) => { // get any previously set cookie for the current tab var gettingCookies = browser.cookies.get({ @@ -22,3 +20,8 @@ function cookieUpdate(tabId, changeInfo, tab) { }); }); } + +// update when the tab is updated +browser.tabs.onUpdated.addListener(cookieUpdate); +// update when the tab is activated +browser.tabs.onActivated.addListener(cookieUpdate); From 62d95d240c6b2a7098918296094a317c027d09d4 Mon Sep 17 00:00:00 2001 From: Will Bamberg Date: Fri, 28 Apr 2017 11:08:34 -0700 Subject: [PATCH 3/3] Get document.html and document.body only when we need them --- cookie-bg-picker/content_scripts/updatebg.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cookie-bg-picker/content_scripts/updatebg.js b/cookie-bg-picker/content_scripts/updatebg.js index f3bd6be..61479bd 100644 --- a/cookie-bg-picker/content_scripts/updatebg.js +++ b/cookie-bg-picker/content_scripts/updatebg.js @@ -1,13 +1,12 @@ -var html = document.querySelector('html'); -var body = document.querySelector('body'); - browser.runtime.onMessage.addListener(updateBg); function updateBg(request, sender, sendResponse) { - if(request.image) { + var html = document.querySelector('html'); + var body = document.querySelector('body'); + if (request.image) { html.style.backgroundImage = 'url(' + request.image + ')'; body.style.backgroundImage = 'url(' + request.image + ')'; - } else if(request.color) { + } else if (request.color) { html.style.backgroundColor = request.color; body.style.backgroundColor = request.color; } else if (request.reset) {