mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-09-20 04:53:43 +02:00
Merge pull request #261 from OfficialNoob/patch-1
Made decodeStoredItem detect item type
This commit is contained in:
commit
77eff12d9b
3 changed files with 29 additions and 17 deletions
|
@ -1,5 +1,8 @@
|
|||
import * as Types from "./types";
|
||||
|
||||
import Config from "./config";
|
||||
// Make the config public for debugging purposes
|
||||
(<any> window).SB = Config;
|
||||
|
||||
import Utils from "./utils";
|
||||
var utils = new Utils({
|
||||
|
|
|
@ -84,13 +84,8 @@ class SBMap<T, U> extends Map {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return Array.from(this.entries());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var Config: SBObject = {
|
||||
/**
|
||||
* Callback function when an option is updated
|
||||
|
@ -131,14 +126,14 @@ var Config: SBObject = {
|
|||
|
||||
/**
|
||||
* A SBMap cannot be stored in the chrome storage.
|
||||
* This data will be encoded into an array instead as specified by the toJSON function.
|
||||
* This data will be encoded into an array instead
|
||||
*
|
||||
* @param data
|
||||
*/
|
||||
function encodeStoredItem(data) {
|
||||
// if data is SBMap convert to json for storing
|
||||
if(!(data instanceof SBMap)) return data;
|
||||
return JSON.stringify(data);
|
||||
return Array.from(data.entries());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -148,18 +143,30 @@ function encodeStoredItem(data) {
|
|||
* @param {*} data
|
||||
*/
|
||||
function decodeStoredItem(id: string, data) {
|
||||
if(typeof data !== "string") return data;
|
||||
|
||||
try {
|
||||
let str = JSON.parse(data);
|
||||
|
||||
if(!Array.isArray(str)) return data;
|
||||
return new SBMap(id, str);
|
||||
} catch(e) {
|
||||
if (!Config.defaults[id]) return data;
|
||||
|
||||
// If all else fails, return the data
|
||||
return data;
|
||||
if (Config.defaults[id] instanceof SBMap) {
|
||||
try {
|
||||
let jsonData: any = data;
|
||||
|
||||
// Check if data is stored in the old format for SBMap (a JSON string)
|
||||
if (typeof data === "string") {
|
||||
try {
|
||||
jsonData = JSON.parse(data);
|
||||
} catch(e) {
|
||||
// Continue normally (out of this if statement)
|
||||
}
|
||||
}
|
||||
|
||||
if (!Array.isArray(jsonData)) return data;
|
||||
return new SBMap(id, jsonData);
|
||||
} catch(e) {
|
||||
console.error("Failed to parse SBMap: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
// If all else fails, return the data
|
||||
return data;
|
||||
}
|
||||
|
||||
function configProxy(): any {
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import Config from "./config";
|
||||
// Make the config public for debugging purposes
|
||||
(<any> window).SB = Config;
|
||||
|
||||
import Utils from "./utils";
|
||||
var utils = new Utils();
|
||||
|
|
Loading…
Reference in a new issue