FloatingWindow

Adds FloatingWindow

当前为 2023-10-15 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/477480/1265097/FloatingWindow.js

  1. // ==UserScript==
  2. // @name FloatingWindow
  3. // @version 1.0
  4. // @grant GM_addStyle
  5. // @grant GM_getValue
  6. // @grant GM_setValue
  7. // @description Adds FloatingWindow
  8. // ==/UserScript==
  9.  
  10. GM_addStyle(`
  11. .floatingWindowContainer {
  12. width:200px;
  13. position:fixed;
  14. opacity:0.5;
  15. }
  16. .floatingWindowTitleBar {
  17. background-color:#fff;
  18. font-weight:bold;
  19. text-align:center;
  20. cursor:pointer;
  21. }
  22. .floatingWindowContainer.active {
  23. opacity:1;
  24. }
  25. `);
  26.  
  27. class FloatingWindow {
  28. constructor(title) {
  29. this.container = document.createElement("div");
  30. this.container.className = "floatingWindowContainer";
  31. this.container.id = title.replaceAll(/\W+/g,"_");
  32. this.windowPosition = GM_getValue(`${this.container.id}_position`,{x:window.innerWidth*0.8,y:100});
  33. this.container.style.left = `${this.windowPosition.x}px`;
  34. this.container.style.top = `${this.windowPosition.y}px`;
  35. this.titleBar = document.createElement("div");
  36. this.titleBar.className = "floatingWindowTitleBar";
  37. this.titleBar.appendChild(document.createTextNode(title));
  38. this.titleBar.addEventListener("pointerdown",(e)=>{this.pointerDownHandler(e);});
  39. this.titleBar.addEventListener("pointermove",(e)=>{this.pointerMoveHandler(e);});
  40. this.titleBar.addEventListener("pointerleave",(e)=>{this.pointerMoveHandler(e);});
  41. this.titleBar.addEventListener("pointerout",(e)=>{this.pointerMoveHandler(e);});
  42. this.titleBar.addEventListener("pointerup",(e)=>{this.pointerUpHandler(e);});
  43. this.container.appendChild(this.titleBar);
  44. this.body = document.createElement("div");
  45. this.body.className = "floatingWindowBody";
  46. this.windowOpen = GM_getValue(`${this.container.id}_open`,true);
  47. if (this.windowOpen) this.container.classList.add("active");
  48. this.body.style.display = this.windowOpen ? "block" : "none";
  49. this.container.appendChild(this.body);
  50. document.body.appendChild(this.container);
  51. this.pointerValues = {
  52. dragging:false,
  53. newPosition:null,
  54. positionOffset:null
  55. };
  56. }
  57. toggleWindow() {
  58. this.windowOpen = !this.windowOpen;
  59. this.container.classList.toggle("active");
  60. GM_setValue(`${this.container.id}_open`,this.windowOpen);
  61. this.body.style.display = this.windowOpen ? "block" : "none";
  62. }
  63. pointerDownHandler(e) {
  64. e.preventDefault();
  65. this.pointerValues.positionOffset = {
  66. x:e.clientX - this.windowPosition.x,
  67. y:e.clientY - this.windowPosition.y
  68. };
  69. }
  70. pointerMoveHandler(e) {
  71. if (this.pointerValues.positionOffset) {
  72. e.preventDefault();
  73. this.pointerValues.newPosition = {
  74. x:e.clientX - this.pointerValues.positionOffset.x,
  75. y:e.clientY - this.pointerValues.positionOffset.y
  76. };
  77. if (!this.pointerValues.dragging) {
  78. if (Math.abs(this.pointerValues.newPosition.x-this.windowPosition.x) > 10 || Math.abs(this.pointerValues.newPosition.y-this.windowPosition.y) > 10) this.pointerValues.dragging = true;
  79. }
  80. if (this.pointerValues.dragging) {
  81. this.container.style.left = `${this.pointerValues.newPosition.x}px`;
  82. this.container.style.top = `${this.pointerValues.newPosition.y}px`;
  83. }
  84. }
  85. }
  86. pointerUpHandler(e) {
  87. e.preventDefault();
  88. if (this.pointerValues.dragging) {
  89. this.pointerValues.newPosition = {
  90. x:e.clientX - this.pointerValues.positionOffset.x,
  91. y:e.clientY - this.pointerValues.positionOffset.y
  92. };
  93. this.container.style.left = `${this.pointerValues.newPosition.x}px`;
  94. this.container.style.top = `${this.pointerValues.newPosition.y}px`;
  95. this.windowPosition = this.pointerValues.newPosition;
  96. GM_setValue(`${this.container.id}_position`,this.windowPosition);
  97. this.pointerValues.dragging = false;
  98. } else {
  99. this.toggleWindow();
  100. }
  101. this.pointerValues.positionOffset = this.pointerValues.newPosition = null;
  102. }
  103. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址