|
|
@ -10,17 +10,19 @@ |
|
|
|
*/ |
|
|
|
class ScriptAndStyleContext { |
|
|
|
/** |
|
|
|
* @property {map<string, map<string,string>>} functions |
|
|
|
* @property {Map<string, Map<string, string>>} #css |
|
|
|
* @type {Map<string, Map<string, string>>} #css |
|
|
|
*/ |
|
|
|
#css; |
|
|
|
/** |
|
|
|
* @property {map<string, FunctionStoreBuffer>} functions |
|
|
|
* @property {Map<string, FunctionStoreBuffer>} #functions |
|
|
|
* @type {Map<string, FunctionStoreBuffer>} #functions |
|
|
|
*/ |
|
|
|
#functions; |
|
|
|
|
|
|
|
constructor() { |
|
|
|
this.#functions = {}; |
|
|
|
this.#css = {}; |
|
|
|
this.#functions = new Map(); |
|
|
|
this.#css = new Map(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -29,7 +31,7 @@ class ScriptAndStyleContext { |
|
|
|
* @returns a name for a function (e.g. for storing) |
|
|
|
*/ |
|
|
|
getFunctionName(nameAddition = "") { |
|
|
|
return `func${this.#functions.length}${nameAddition}`; |
|
|
|
return `func${this.#functions.size}${nameAddition}`; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -54,7 +56,7 @@ class ScriptAndStyleContext { |
|
|
|
].find(e => e !== ''); |
|
|
|
|
|
|
|
/* deal with name already present */ |
|
|
|
let functionNames = Object.keys(this.#functions); |
|
|
|
let functionNames = this.#functions.keys; |
|
|
|
if (functionNames.includes(registrationName)) { |
|
|
|
registrationName = resolveOverwrite(registrationName, this.#functions, overwriteBehaviour); |
|
|
|
} |
|
|
@ -62,7 +64,7 @@ class ScriptAndStyleContext { |
|
|
|
/* clear function text */ |
|
|
|
let clearedFuncText = clearFunctionDeclarationText(fun); |
|
|
|
|
|
|
|
this.#functions[registrationName] = new FunctionStoreBuffer(clearedFuncText); |
|
|
|
this.#functions.set(registrationName, new FunctionStoreBuffer(clearedFuncText)); |
|
|
|
|
|
|
|
return registrationName; |
|
|
|
} |
|
|
@ -79,7 +81,7 @@ class ScriptAndStyleContext { |
|
|
|
*/ |
|
|
|
registerRepeatingFunction(fun, interval, underTheName = '', overwriteBehaviour = OverwriteBehaviour.RENAME, args = []) { |
|
|
|
let registrationName = this.registerPageFunction(fun, underTheName, overwriteBehaviour); |
|
|
|
let fsb = this.#functions[registrationName]; |
|
|
|
let fsb = this.#functions.get(registrationName); |
|
|
|
fsb.repeats = true; |
|
|
|
fsb.interval = interval; |
|
|
|
fsb.args = args; |
|
|
@ -98,7 +100,7 @@ class ScriptAndStyleContext { |
|
|
|
*/ |
|
|
|
executeAfterLoad(fun, delay = 1000, underTheName = '', overwriteBehaviour = OverwriteBehaviour.RENAME, interval = -1, args = []) { |
|
|
|
let registrationName = this.registerPageFunction(fun, underTheName, overwriteBehaviour); |
|
|
|
let fsb = this.#functions[registrationName]; |
|
|
|
let fsb = this.#functions.get(registrationName); |
|
|
|
|
|
|
|
fsb.execAfterStart = true; |
|
|
|
fsb.delay = delay; |
|
|
@ -118,7 +120,7 @@ class ScriptAndStyleContext { |
|
|
|
* @returns a name for a styling (e.g. for storing) |
|
|
|
*/ |
|
|
|
getStyleName(nameAddition = "") { |
|
|
|
return `styling${this.#css.length}${nameAddition}`; |
|
|
|
return `styling${this.#css.size}${nameAddition}`; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -132,12 +134,12 @@ class ScriptAndStyleContext { |
|
|
|
* @param {map<string, string>|Modifier} styleRuleMap The Styling rules/values |
|
|
|
*/ |
|
|
|
registerStyling(elementIdentifier, styleRuleMap) { |
|
|
|
if(styleRuleMap instanceof Modifier){ |
|
|
|
if (styleRuleMap instanceof Modifier) { |
|
|
|
styleRuleMap = styleRuleMap._modifications; |
|
|
|
} |
|
|
|
|
|
|
|
if (!Object.keys(this.#css).includes(elementIdentifier)) { |
|
|
|
this.#css[elementIdentifier] = styleRuleMap |
|
|
|
if (!this.#css.has(elementIdentifier)) { |
|
|
|
this.#css.set(elementIdentifier, styleRuleMap); |
|
|
|
} |
|
|
|
return elementIdentifier; |
|
|
|
} |
|
|
@ -156,13 +158,12 @@ class ScriptAndStyleContext { |
|
|
|
/* generate style tag and fill it with stored stylings */ |
|
|
|
let styleTag = document.createElement('style'); |
|
|
|
|
|
|
|
Object.entries(this.#css) |
|
|
|
.forEach((tuple) => { |
|
|
|
styleTag.innerText += `${tuple[0]} {${Object.entries(tuple[1]) |
|
|
|
.map(style => style[0] + ": " + style[1] + "; ") |
|
|
|
.join(" ") |
|
|
|
}} `;
|
|
|
|
}); |
|
|
|
for (const tuple of this.#css.entries()) { |
|
|
|
styleTag.innerText += `${tuple[0]} {${Object.entries(tuple[1]) |
|
|
|
.map(style => style[0] + ": " + style[1] + "; ") |
|
|
|
.join(" ") |
|
|
|
}} `;
|
|
|
|
} |
|
|
|
|
|
|
|
head.appendChild(styleTag); |
|
|
|
|
|
|
@ -177,28 +178,26 @@ class ScriptAndStyleContext { |
|
|
|
containersTag.innerText += 'const repeated = {}; '; |
|
|
|
head.appendChild(containersTag); |
|
|
|
|
|
|
|
if (this.#functions.length > 0) { |
|
|
|
if (this.#functions.size > 0) { |
|
|
|
let funcTag = document.createElement('script'); |
|
|
|
Object.entries(this.#functions) |
|
|
|
.forEach(tuple => { |
|
|
|
let regName = tuple[0]; |
|
|
|
let fsb = tuple[1]; |
|
|
|
funcTag.innerText += getScriptTagInjectionText(fsb.func, regName); |
|
|
|
|
|
|
|
if (fsb.repeats && !fsb.execAfterStart) { |
|
|
|
repeated[regName] = setInterval(regName, fsb.interval, fsb.args); |
|
|
|
} |
|
|
|
|
|
|
|
if (!fsb.repeats && fsb.execAfterStart) { |
|
|
|
delayed[regName] = setTimeout(regName, fsb.interval, fsb.args); |
|
|
|
} |
|
|
|
|
|
|
|
if (fsb.repeats && fsb.execAfterStart) { |
|
|
|
repeated[regName] = setInterval(regName, fsb.interval, fsb.args); |
|
|
|
delayed[regName] = setTimeout(repeated[regName], fsb.delay, fsb.args); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
for (const tuple of this.#functions.entries) { |
|
|
|
let regName = tuple[0]; |
|
|
|
let fsb = tuple[1]; |
|
|
|
funcTag.innerText += getScriptTagInjectionText(fsb.func, regName); |
|
|
|
|
|
|
|
if (fsb.repeats && !fsb.execAfterStart) { |
|
|
|
repeated[regName] = setInterval(regName, fsb.interval, fsb.args); |
|
|
|
} |
|
|
|
|
|
|
|
if (!fsb.repeats && fsb.execAfterStart) { |
|
|
|
delayed[regName] = setTimeout(regName, fsb.interval, fsb.args); |
|
|
|
} |
|
|
|
|
|
|
|
if (fsb.repeats && fsb.execAfterStart) { |
|
|
|
repeated[regName] = setInterval(regName, fsb.interval, fsb.args); |
|
|
|
delayed[regName] = setTimeout(repeated[regName], fsb.delay, fsb.args); |
|
|
|
} |
|
|
|
} |
|
|
|
head.appendChild(funcTag); |
|
|
|
} |
|
|
|
} |
|
|
|