1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2024-12-04 22:42:49 +01:00
TweetDuck/resources/Content/tweetdeck/globals/inject_mustache.js

48 lines
1.0 KiB
JavaScript

import { TD } from "../../api/td.js";
import { crashDebug } from "../../api/utils.js";
/**
* Injects custom HTML into mustache templates.
* @param {string} name
* @param {"replace"|"append"|"prepend"} operation
* @param {string} search
* @param {string} custom
* @returns {boolean}
*/
export function injectMustache(name, operation, search, custom) {
let replacement;
switch (operation) {
case "replace":
replacement = custom;
break;
case "append":
replacement = search + custom;
break;
case "prepend":
replacement = custom + search;
break;
default:
throw "Invalid mustache injection operation. Only 'replace', 'append', 'prepend' are supported.";
}
const prev = TD.mustaches?.[name];
if (!prev) {
crashDebug("Mustache injection is referencing an invalid mustache: " + name);
return false;
}
TD.mustaches[name] = prev.replace(search, replacement);
if (prev === TD.mustaches[name]) {
crashDebug("Mustache injection had no effect: " + name);
return false;
}
return true;
}