constants.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /** URL search parameters at the earliest possible time */
  18. export const initialParams = new URL(location.href).searchParams;
  19. /** Names of platforms by value of {@linkcode host} */
  20. export const platformNames: Record<typeof host, string> = {
  21. github: "GitHub",
  22. greasyfork: "GreasyFork",
  23. openuserjs: "OpenUserJS",
  24. };
  25. /** Default compression format used throughout BYTM */
  26. export const compressionFormat: CompressionFormat = "deflate-raw";
  27. /** Whether sessionStorage is available and working */
  28. export const sessionStorageAllowed =
  29. typeof sessionStorage?.setItem !== "undefined"
  30. && (() => {
  31. try {
  32. const key = `_bytm_test_${randomId(4)}`;
  33. sessionStorage.setItem(key, "test");
  34. sessionStorage.removeItem(key);
  35. return true;
  36. }
  37. catch {
  38. return false;
  39. }
  40. })();
  41. /**
  42. * How much info should be logged to the devtools console
  43. * 0 = Debug (show everything) or 1 = Info (show only important stuff)
  44. */
  45. export const defaultLogLevel: LogLevel = mode === "production" ? LogLevel.Info : LogLevel.Debug;
  46. /** Info about the userscript, parsed from the userscript header (tools/post-build.js) */
  47. export const scriptInfo = {
  48. name: GM.info.script.name,
  49. version: GM.info.script.version,
  50. namespace: GM.info.script.namespace,
  51. } as const;