constants.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { randomId } from "@sv443-network/userutils";
  2. import { LogLevel } from "./types";
  3. const modeRaw = "#{{MODE}}";
  4. const branchRaw = "#{{BRANCH}}";
  5. const hostRaw = "#{{HOST}}";
  6. const buildNumberRaw = "#{{BUILD_NUMBER}}";
  7. /** The mode in which the script was built (production or development) */
  8. export const mode = (modeRaw.match(/^#{{.+}}$/) ? "production" : modeRaw) as "production" | "development";
  9. /** The branch to use in various URLs that point to the GitHub repo */
  10. export const branch = (branchRaw.match(/^#{{.+}}$/) ? "main" : branchRaw) as "main" | "develop";
  11. /** Path to the GitHub repo */
  12. export const repo = "Sv443/BetterYTM";
  13. /** Which host the userscript was installed from */
  14. export const host = (hostRaw.match(/^#{{.+}}$/) ? "github" : hostRaw) as "github" | "greasyfork" | "openuserjs";
  15. /** The build number of the userscript */
  16. export const buildNumber = (buildNumberRaw.match(/^#{{.+}}$/) ? "BUILD_ERROR!" : buildNumberRaw) as string; // asserted as generic string instead of literal
  17. /** Names of platforms by value of {@linkcode host} */
  18. export const platformNames: Record<typeof host, string> = {
  19. github: "GitHub",
  20. greasyfork: "GreasyFork",
  21. openuserjs: "OpenUserJS",
  22. };
  23. /** Default compression format used throughout BYTM */
  24. export const compressionFormat: CompressionFormat = "deflate-raw";
  25. export const sessionStorageAllowed =
  26. typeof sessionStorage !== "undefined"
  27. && (() => {
  28. try {
  29. const key = `_bytm_${randomId(4)}`;
  30. sessionStorage.setItem(key, "test");
  31. sessionStorage.removeItem(key);
  32. return true;
  33. }
  34. catch {
  35. return false;
  36. }
  37. })();
  38. /**
  39. * How much info should be logged to the devtools console
  40. * 0 = Debug (show everything) or 1 = Info (show only important stuff)
  41. */
  42. export const defaultLogLevel: LogLevel = mode === "production" ? LogLevel.Info : LogLevel.Debug;
  43. /** Info about the userscript, parsed from the userscript header (tools/post-build.js) */
  44. export const scriptInfo = {
  45. name: GM.info.script.name,
  46. version: GM.info.script.version,
  47. namespace: GM.info.script.namespace,
  48. } as const;