constants.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /** Whether sessionStorage is available and working */
  26. export const sessionStorageAllowed =
  27. typeof sessionStorage?.setItem !== "undefined"
  28. && (() => {
  29. try {
  30. const key = `_bytm_test_${randomId(4)}`;
  31. sessionStorage.setItem(key, "test");
  32. sessionStorage.removeItem(key);
  33. return true;
  34. }
  35. catch {
  36. return false;
  37. }
  38. })();
  39. /**
  40. * How much info should be logged to the devtools console
  41. * 0 = Debug (show everything) or 1 = Info (show only important stuff)
  42. */
  43. export const defaultLogLevel: LogLevel = mode === "production" ? LogLevel.Info : LogLevel.Debug;
  44. /** Info about the userscript, parsed from the userscript header (tools/post-build.js) */
  45. export const scriptInfo = {
  46. name: GM.info.script.name,
  47. version: GM.info.script.version,
  48. namespace: GM.info.script.namespace,
  49. } as const;