Don't notify normal tabs if style contents hasn't changed

This commit is contained in:
tophf 2017-03-15 14:55:20 +03:00
parent e807d41eb7
commit 913df00f35
2 changed files with 15 additions and 7 deletions

View File

@ -1,12 +1,15 @@
// keep message channel open for sendResponse in chrome.runtime.onMessage listener
const KEEP_CHANNEL_OPEN = true;
const OWN_ORIGIN = chrome.runtime.getURL('');
function notifyAllTabs(request) {
chrome.windows.getAll({populate: true}, function(windows) {
windows.forEach(function(win) {
win.tabs.forEach(function(tab) {
chrome.tabs.sendMessage(tab.id, request);
updateIcon(tab);
chrome.windows.getAll({populate: true}, windows => {
windows.forEach(win => {
win.tabs.forEach(tab => {
if (request.codeIsUpdated !== false || tab.url.startsWith(OWN_ORIGIN)) {
chrome.tabs.sendMessage(tab.id, request);
updateIcon(tab);
}
});
});
});

View File

@ -105,11 +105,13 @@ function saveStyle(style, {notify = true} = {}) {
if (style.id) {
style.id = Number(style.id);
os.get(style.id).onsuccess = eventGet => {
style = Object.assign({}, eventGet.target.result, style);
const oldStyle = Object.assign({}, eventGet.target.result);
const codeIsUpdated = !styleSectionsEqual(style, oldStyle);
style = Object.assign(oldStyle, style);
os.put(style).onsuccess = eventPut => {
style.id = style.id || eventPut.target.result;
if (notify) {
notifyAllTabs({method: 'styleUpdated', style});
notifyAllTabs({method: 'styleUpdated', style, codeIsUpdated});
}
invalidateCache(notify);
resolve(style);
@ -576,6 +578,9 @@ function getSync() {
}
function styleSectionsEqual(styleA, styleB) {
if (!styleA.sections || !styleB.sections) {
return undefined;
}
if (styleA.sections.length != styleB.sections.length) {
return false;
}