constants.ts 2.3 KB

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