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

View File

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