Skip to content

Commit

Permalink
fixed backup related issue with blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Kwaschny committed May 3, 2023
1 parent d9700c8 commit e82a735
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 83 deletions.
2 changes: 1 addition & 1 deletion _locales/de/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"message": "Unwanted Twitch:\nSie sind dabei alle \"X\" Buttons auszublenden.\n\nSie können die Sichtbarkeit über das 👁 Augen-Symbol jederzeit wieder umschalten."
},
"alert_StorageQuota": {
"message": "Unwanted Twitch:\nDie Sperrliste ist voll. Es können keine weiteren Einträge hinzugefügt werden, solange die Synchronisierung stattfinden soll.\n\nDeaktivieren Sie \"Sperrliste in Cloud synchronisieren\" in den Einstellungen, um weitere Einträge hinzufügen zu können."
"message": "Unwanted Twitch:\nDie Sperrliste ist voll und kann nicht länger über die Cloud synchronisiert werden."
},
"alert_StorageThrottle": {
"message": "Unwanted Twitch:\nDer Synchronisierungsdienst ist derzeit überlastet.\nBitte warten Sie ein paar Minuten, starten den Browser neu und versuchen es dann noch einmal."
Expand Down
2 changes: 1 addition & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"message": "Unwanted Twitch:\nYou are about to hide all \"X\" buttons.\n\nYou can toggle the visibility by clicking on the 👁 eye icon again."
},
"alert_StorageQuota": {
"message": "Unwanted Twitch:\nThe blacklist is full. You cannot add more items if you want to keep synchronization enabled.\n\nDisable \"Synchronize Blacklist via Cloud\" under settings to be able to add more items to the blacklist."
"message": "Unwanted Twitch:\nThe blacklist is full and can no longer be synchronized."
},
"alert_StorageThrottle": {
"message": "Unwanted Twitch:\nThe storage service is currently busy.\nPlease wait a few minutes, restart your browser and try again."
Expand Down
2 changes: 1 addition & 1 deletion _locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"message": "Unwanted Twitch:\nYou are about to hide all \"X\" buttons.\n\nYou can toggle the visibility by clicking on the 👁 eye icon again."
},
"alert_StorageQuota": {
"message": "Unwanted Twitch:\nLa lista negra está llena. No puedes añadir más ítems si quieres mantener la sincronización activada.\n\nDesactiva \"Sincronizar la Lista Negra en la Nube\" en los ajustes para poder añadir más ítems a la lista negra."
"message": "Unwanted Twitch:\nLa lista negra está llena y ya no se puede sincronizar."
},
"alert_StorageThrottle": {
"message": "Unwanted Twitch:\nEl servicio de almacenamiento está ocupado en estos momentos.\nPor favor espera unos minutos, reinicia tu navegador y prueba otra vez."
Expand Down
135 changes: 55 additions & 80 deletions scripts/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

// collection of blacklisted items, serves as local cache
let storedBlacklistedItems = {};
let backupBlacklistedItems = {};

// internal cache to improve matching performance
let cacheExactTerms = {};
Expand Down Expand Up @@ -2198,7 +2199,7 @@
}

/**
* Event that is emitted by hide buttons on cards in the directory of the current page. Returns if the tag was removed.
* Event that is emitted by hide buttons on cards in the directory of the current page.
*/
function onHideItem(item) {
logTrace('invoking onHideItem($)', item);
Expand All @@ -2216,20 +2217,10 @@

// update storage
putBlacklistedItems(storedBlacklistedItems);

// remove item
if (removeDirectoryItem(item) === true) {

logVerbose('Removed item in directory due to being blacklisted:', item);

return true;
}

return false;
}

/**
* Event that is emitted by hide buttons on tags in the directory of the current page. Returns if the tag was removed.
* Event that is emitted by hide buttons on tags in the directory of the current page.
*/
function onHideTag(item, tag) {
logTrace('invoking onHideTag($, $)', item, tag);
Expand All @@ -2247,16 +2238,6 @@

// update storage
putBlacklistedItems(storedBlacklistedItems);

// remove item
if (removeDirectoryItem(item) === true) {

logVerbose('Removed item in directory due to being blacklisted via tag:', item, tag);

return true;
}

return false;
}

/**
Expand Down Expand Up @@ -2303,12 +2284,10 @@
function initBlacklistedItems(collection) {
logTrace('invoking initBlacklistedItems($)', collection);

const itemTypesAsObject = [
const itemTypes = [
'categories',
'channels',
'tags'
];
const itemTypesAsArray = [
'tags',
'titles'
];

Expand All @@ -2318,30 +2297,17 @@
collection = {};
}

// prepare all item types in collection (object)
const itemTypesAsObjectLength = itemTypesAsObject.length;
for (let i = 0; i < itemTypesAsObjectLength; i++) {
const itemTypesLength = itemTypes.length;
for (let i = 0; i < itemTypesLength; i++) {

let itemType = itemTypesAsObject[i];
let itemType = itemTypes[i];

if (collection[itemType] === undefined) {

collection[itemType] = {};
}
}

// prepare all item types in collection (array)
const itemTypesAsArrayLength = itemTypesAsArray.length;
for (let i = 0; i < itemTypesAsArrayLength; i++) {

let itemType = itemTypesAsArray[i];

if (collection[itemType] === undefined) {

collection[itemType] = [];
}
}

return collection;
}

Expand Down Expand Up @@ -2428,52 +2394,58 @@
cacheLooseTerms = {};
cacheRegExpTerms = {};

const itemsKeys = [ 'categories', 'channels', 'tags', 'titles' ];
for (const itemsKey of itemsKeys) {
const itemTypes = [
'categories',
'channels',
'tags',
'titles'
];

for (const itemType of itemTypes) {

let terms;
if (Array.isArray(items[itemsKey])) {
if (Array.isArray(items[itemType])) {

terms = items[itemsKey];
terms = items[itemType];

} else {

terms = Object.keys(items[itemsKey]);
terms = Object.keys(items[itemType]);
}

for (const term of terms) {

if (isExactTerm(term)) {

if (cacheExactTerms[itemsKey] === undefined) {
cacheExactTerms[itemsKey] = [];
if (cacheExactTerms[itemType] === undefined) {
cacheExactTerms[itemType] = [];
}

cacheExactTerms[itemsKey].push(
cacheExactTerms[itemType].push(
term.substring(1, (term.length -1))
);
continue;
}

if (isLooseTerm(term)) {

if (cacheLooseTerms[itemsKey] === undefined) {
cacheLooseTerms[itemsKey] = [];
if (cacheLooseTerms[itemType] === undefined) {
cacheLooseTerms[itemType] = [];
}

cacheLooseTerms[itemsKey].push(
cacheLooseTerms[itemType].push(
term.substring(1)
);
continue;
}

if (isRegExpTerm(term)) {

if (cacheRegExpTerms[itemsKey] === undefined) {
cacheRegExpTerms[itemsKey] = [];
if (cacheRegExpTerms[itemType] === undefined) {
cacheRegExpTerms[itemType] = [];
}

cacheRegExpTerms[itemsKey].push( toRegExp(term) );
cacheRegExpTerms[itemType].push( toRegExp(term) );
continue;
}
}
Expand All @@ -2485,30 +2457,23 @@
* Stores all blacklisted items in the storage.
*/
function putBlacklistedItems(items, callback, attemptRecovery) {
logTrace('invoking putBlacklistedItems($, $, $)', items, callback, attemptRecovery);

if (attemptRecovery === false) {
logVerbose('invoking putBlacklistedItems($, $, $)', items, callback, attemptRecovery);

logWarn('Restoring backup:', items);
}
getStorageMode(function(mode) {

// prepare backup of current items
const backupItems = cloneBlacklistItems(items);
const isSync = (mode === 'sync');

// clean up bug, see https://github.com/kwaschny/unwanted-twitch/issues/29
delete items['categories'][''];
delete items['channels'][''];
delete items['tags'][''];
delete items['titles'][''];
if (attemptRecovery === false) {

// synchronize new items among tabs
syncBlacklistedItems(items);
logWarn('Restoring backup:', items);
}

let dataToStore = { 'blacklistedItems': items };
// prepare backup of current items
const backupItems = cloneBlacklistItems(items);

getStorageMode(function(mode) {
let dataToStore = { 'blacklistedItems': items };

if (mode === 'sync') {
if (isSync) {

const requiredSize = measureStoredSize(dataToStore);

Expand Down Expand Up @@ -2541,7 +2506,7 @@

if (attemptRecovery !== false) {

const suffix = ('\n\nBrowser\'s runtime.lastError reports:\n' + error.message);
const suffix = ('\n\nStorage Service Error:\n' + error.message);

if (error.message.indexOf('QUOTA_BYTES') >= 0) {

Expand All @@ -2556,18 +2521,27 @@
alert(chrome.i18n.getMessage('alert_StorageIssue') + suffix);
}

// something went wrong, restore the backup
logWarn('Attempting to restore backup:', backupBlacklistedItems);
putBlacklistedItems(backupBlacklistedItems, callback, false);
// something went wrong, restore the backup (force local storage)
chrome.storage.local.set({ 'useLocalStorage': true }, function() {

putBlacklistedItems(backupBlacklistedItems, callback, false);
});

return;
}

} else {

// synchronize new items among tabs
syncBlacklistedItems(items);

// update backup cache
backupBlacklistedItems = backupItems;
if (attemptRecovery !== false) {

backupBlacklistedItems = backupItems;
logVerbose('Created backup of blacklist:', backupBlacklistedItems);
}

logVerbose('Created backup of blacklist:', backupBlacklistedItems);
logInfo('Added to blacklist:', items);
}

Expand Down Expand Up @@ -2613,10 +2587,11 @@

// cache blacklisted items from storage
storedBlacklistedItems = blacklistedItems;
backupBlacklistedItems = cloneBlacklistItems(blacklistedItems);
modifyBlacklistedItems(blacklistedItems);
logInfo('Blacklist loaded:', blacklistedItems);

backupBlacklistedItems = cloneBlacklistItems(blacklistedItems);

/* BEGIN: root */

const rootNodeSelector = '#root';
Expand Down

0 comments on commit e82a735

Please sign in to comment.