mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-06-01 11:34:04 +02:00
Add TDPF_createStorage as a plugin replacement for localStorage
This commit is contained in:
parent
1e8c62ac25
commit
b4d359d30c
@ -21,6 +21,11 @@
|
|||||||
$TDP.checkFileExists(token, fileNameUser).then(exists => {
|
$TDP.checkFileExists(token, fileNameUser).then(exists => {
|
||||||
let fileName = exists ? fileNameUser : fileNameDefault;
|
let fileName = exists ? fileNameUser : fileNameDefault;
|
||||||
|
|
||||||
|
if (fileName === null){
|
||||||
|
onSuccess && onSuccess({});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
(exists ? $TDP.readFile(token, fileName, true) : $TDP.readFileRoot(token, fileName)).then(contents => {
|
(exists ? $TDP.readFile(token, fileName, true) : $TDP.readFileRoot(token, fileName)).then(contents => {
|
||||||
let obj;
|
let obj;
|
||||||
|
|
||||||
@ -63,4 +68,105 @@
|
|||||||
element: element
|
element: element
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// Block: Setup a function to mimic a Storage object that will be saved in the plugin.
|
||||||
|
//
|
||||||
|
window.TDPF_createStorage = function(pluginObject, onReady){
|
||||||
|
validatePluginObject(pluginObject);
|
||||||
|
|
||||||
|
if ("$storage" in pluginObject){
|
||||||
|
if (pluginObject.$storage !== null){ // set to null while the file is still loading
|
||||||
|
onReady(pluginObject.$storage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Storage{
|
||||||
|
get length(){
|
||||||
|
return Object.keys(this).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
key(index){
|
||||||
|
return Object.keys(this)[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
getItem(key){
|
||||||
|
return this[key] || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
setItem(key, value){
|
||||||
|
this[key] = value;
|
||||||
|
updateFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
removeItem(key){
|
||||||
|
delete this[key];
|
||||||
|
updateFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
clear(){
|
||||||
|
for(key of Object.keys(this)){
|
||||||
|
delete this[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
updateFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
replace(obj, silent){
|
||||||
|
for(let key of Object.keys(this)){
|
||||||
|
delete this[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(let key in obj){
|
||||||
|
this[key] = obj[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!silent){
|
||||||
|
updateFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var storage = new Proxy(new Storage(), {
|
||||||
|
get: function(obj, prop, receiver){
|
||||||
|
const value = obj[prop];
|
||||||
|
return typeof value === "function" ? value.bind(obj) : value;
|
||||||
|
},
|
||||||
|
|
||||||
|
set: function(obj, prop, value){
|
||||||
|
obj.setItem(prop, value);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
deleteProperty: function(obj, prop){
|
||||||
|
obj.removeItem(prop);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
enumerate: function(obj){
|
||||||
|
return Object.keys(obj);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var delay = -1;
|
||||||
|
|
||||||
|
const updateFile = function(){
|
||||||
|
window.clearTimeout(delay);
|
||||||
|
|
||||||
|
delay = window.setTimeout(function(){
|
||||||
|
$TDP.writeFile(pluginObject.$token, ".storage", JSON.stringify(storage));
|
||||||
|
}, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
pluginObject.$storage = null;
|
||||||
|
|
||||||
|
window.TDPF_loadConfigurationFile(pluginObject, ".storage", null, function(obj){
|
||||||
|
storage.replace(obj, true);
|
||||||
|
onReady(pluginObject.$storage = storage);
|
||||||
|
}, function(){
|
||||||
|
onReady(pluginObject.$storage = storage);
|
||||||
|
});
|
||||||
|
};
|
||||||
})();
|
})();
|
||||||
|
Loading…
Reference in New Issue
Block a user