main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. class WebTileService {
  2. #canvas = null;
  3. #title = document.title;
  4. #config = { Background: 'red', Color: 'white', Type: 'scroll', Time: 500 };
  5. #ctx = null;
  6. #linkTag = null;
  7. #img = null;
  8. #timer = null;
  9. constructor(Config) {
  10. if (Config instanceof Object && Object.keys(Config).length) {
  11. this.#config = { ...this.#config, ...Config }
  12. }
  13. this.#init()
  14. }
  15. async #init() {
  16. const icon = document.querySelectorAll('link[rel~=shortcut]')[0]
  17. // img
  18. const img = new Image()
  19. img.style.width = `32px`
  20. img.style.height = `32px`
  21. img.crossOrigin = 'Anonymous'
  22. img.src = icon.href
  23. // canvas 初始化
  24. const canvas = document.createElement('canvas')
  25. canvas.height = 32
  26. canvas.width = 32
  27. this.#canvas = canvas
  28. const ctx = canvas.getContext('2d')
  29. this.#ctx = ctx
  30. // 画底图
  31. const res = await new Promise((res, rej) => {
  32. img.onload = () => res(true)
  33. img.onerror = () => rej(false)
  34. })
  35. if (res) ctx.drawImage(img, 0, 0, 32, 32)
  36. this.#img = img
  37. // dom 初始化
  38. const head = document.getElementsByTagName('head')[0]
  39. const linkTag = document.createElement('link')
  40. linkTag.setAttribute('rel', 'shortcut icon')
  41. linkTag.setAttribute('type', 'image/x-icon')
  42. linkTag.setAttribute('href', canvas.toDataURL('image/png'))
  43. linkTag.setAttribute('id', 'RESETLinkICON')
  44. head.removeChild(icon)
  45. head.appendChild(linkTag)
  46. this.#linkTag = linkTag
  47. }
  48. setTitle(text) {
  49. if (!text) return
  50. const { Type, Time } = this.#config
  51. let title = ''
  52. let scrollTitle = ''
  53. if (this.#timer) clearInterval(this.#timer)
  54. this.#timer = setInterval(() => {
  55. switch (Type) {
  56. case 'scroll':
  57. if (!scrollTitle || !scrollTitle.slice(1)) {
  58. document.title = text
  59. scrollTitle = text
  60. } else {
  61. scrollTitle = scrollTitle.slice(1)
  62. document.title = scrollTitle
  63. }
  64. break;
  65. case 'flicker':
  66. document.title = title === document.title ? text : title
  67. default:
  68. clearInterval(this.#timer)
  69. document.title = this.#title
  70. break;
  71. }
  72. }, Time);
  73. }
  74. setNum(num) {
  75. if (!(num instanceof Number) || !num || !this.#ctx || !this.#linkTag) return
  76. const { Background, Color } = this.#config
  77. this.#ctx.arc(21, 21, 11, 0, 2 * Math.PI)
  78. this.#ctx.fillStyle = Background
  79. this.#ctx.fill();
  80. this.#ctx.textAlign = 'center'
  81. this.#ctx.font = 'normal bold 16px Aria'
  82. this.#ctx.fillStyle = Color
  83. this.#ctx.fillText(num > 99 ? '・・' : num, 21, 27)
  84. if (num <= 100) this.#linkTag.setAttribute('href', this.#canvas.toDataURL('image/png'));
  85. }
  86. clearNum() {
  87. this.#ctx.drawImage(this.#img, 0, 0, 32, 32)
  88. this.#linkTag.setAttribute('href', this.#canvas.toDataURL('image/png'));
  89. }
  90. clearTitle() {
  91. clearTimeout(this.#timer)
  92. document.title = this.#title
  93. }
  94. }