mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 14:28:33 +02:00
Merge branch 'issue-197'
This commit is contained in:
@@ -1,26 +1,18 @@
|
||||
/* 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) => {
|
||||
/* 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});
|
||||
@@ -28,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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -36,5 +36,12 @@
|
||||
|
||||
"background": {
|
||||
"scripts": ["background_scripts/background.js"]
|
||||
}
|
||||
},
|
||||
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": ["<all_urls>"],
|
||||
"js": ["content_scripts/updatebg.js"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user