mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-06-07 10:53:10 +03:00
Add dom.js for DOM manipulation, timers, and download trigger
This commit is contained in:
parent
265e8bf29d
commit
c4f4180e76
91
src/tracker/dom.js
Normal file
91
src/tracker/dom.js
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
var DOM = (function(){
|
||||||
|
var setupTimerFunction = function(setFunc, clearFunc, callback, time){
|
||||||
|
var obj = {
|
||||||
|
cancel: function(){
|
||||||
|
if (this.isActive){
|
||||||
|
clearFunc(this.id);
|
||||||
|
this.isActive = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
start: function(){
|
||||||
|
if (!this.isActive){
|
||||||
|
this.id = setFunc(this._callback.bind(obj), this._time);
|
||||||
|
this.isActive = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
_callback: callback,
|
||||||
|
_time: time
|
||||||
|
};
|
||||||
|
|
||||||
|
obj.start();
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
/*
|
||||||
|
* Returns a child element by its ID. Parent defaults to the entire document.
|
||||||
|
*/
|
||||||
|
id: function(id, parent){
|
||||||
|
return (parent || document).getElementById(id);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns an array of all child elements containing the specified class. Parent defaults to the entire document.
|
||||||
|
*/
|
||||||
|
cls: function(cls, parent){
|
||||||
|
return Array.prototype.slice.call((parent || document).getElementsByClassName(cls));
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns an array of all child elements that have the specified tag. Parent defaults to the entire document.
|
||||||
|
*/
|
||||||
|
tag: function(tag, parent){
|
||||||
|
return Array.prototype.slice.call((parent || document).getElementsByTagName(tag));
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Creates a new style element with the specified CSS contents, and returns a function that deletes the element.
|
||||||
|
*/
|
||||||
|
createStyle: function(style){
|
||||||
|
var ele = document.createElement("style");
|
||||||
|
ele.setAttribute("type", "text/css");
|
||||||
|
|
||||||
|
document.head.appendChild(ele);
|
||||||
|
ele.sheet.insertRule(style, 0);
|
||||||
|
|
||||||
|
return function(){
|
||||||
|
document.head.removeChild(ele);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Runs a callback function after a set amount of time. Returns an object which contains several functions and properties for easy management.
|
||||||
|
*/
|
||||||
|
setTimer: function(callback, timeout){
|
||||||
|
return setupTimerFunction(window.setTimeout, window.clearTimeout, callback, timeout);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Runs a callback function periodically after a set amount of time. Returns an object which contains several functions and properties for easy management.
|
||||||
|
*/
|
||||||
|
setInterval: function(callback, interval){
|
||||||
|
return setupTimerFunction(window.setInterval, window.clearInterval, callback, interval);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Triggers a UTF-8 text file download.
|
||||||
|
*/
|
||||||
|
downloadTextFile: function(fileName, fileContents){
|
||||||
|
var ele = document.createElement("a");
|
||||||
|
ele.setAttribute("href", "data:text/plain;charset=utf-8,"+encodeURIComponent(fileContents));
|
||||||
|
ele.setAttribute("download", fileName);
|
||||||
|
ele.style.display = "none";
|
||||||
|
|
||||||
|
document.body.appendChild(ele);
|
||||||
|
ele.click();
|
||||||
|
document.body.removeChild(ele);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
Loading…
x
Reference in New Issue
Block a user