FloatingWindow

Adds FloatingWindow

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

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

  1. // ==UserScript==
  2. // @name FloatingWindow
  3. // @version 1.1
  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. z-index: 9999;
  16. }
  17. .floatingWindowTitleBar {
  18. background-color:#fff;
  19. font-weight:bold;
  20. text-align:center;
  21. cursor:pointer;
  22. }
  23. .floatingWindowContainer.active {
  24. opacity:1;
  25. z-index: 10000;
  26. }
  27. .floatingWindowBody{
  28. flex-wrap:wrap;
  29. }
  30.  
  31. `);
  32.  
  33. class FloatingWindow {
  34. constructor(title,settings = {}) {
  35. this.settings = {
  36. position:settings?.position || {x:window.innerWidth*0.8,y:100},
  37. open: settings.hasOwnProperty("open") ? settings.open : true,
  38. bodyDisplay:settings?.bodyDisplay || "block"
  39. }
  40. this.container = document.createElement("div");
  41. this.container.className = "floatingWindowContainer";
  42. this.container.id = title.replaceAll(/\W+/g,"_");
  43. this.windowPosition = GM_getValue(`${this.container.id}_position`,this.settings.position);
  44. this.container.style.left = `${this.windowPosition.x}px`;
  45. this.container.style.top = `${this.windowPosition.y}px`;
  46. this.titleBar = document.createElement("div");
  47. this.titleBar.className = "floatingWindowTitleBar";
  48. this.titleBar.appendChild(document.createTextNode(title));
  49. this.titleBar.addEventListener("click",()=>{this.toggleWindow();})
  50. this.titleBar.addEventListener("dragstart",(e)=>{this.dragStartHandler(e);})
  51. this.titleBar.addEventListener("drag",(e)=>{this.dragHandler(e);})
  52. this.titleBar.addEventListener("dragend",(e)=>{this.dragEndHandler(e);})
  53. document.body.addEventListener("dragenter",(e)=>{
  54. if (this.pointerValues.positionOffset) {
  55. e.dataTransfer.dropEffect = "move";
  56. }
  57. })
  58. // this.titleBar.addEventListener("pointerdown",(e)=>{this.pointerDownHandler(e);});
  59. // this.titleBar.addEventListener("pointermove",(e)=>{this.pointerMoveHandler(e);});
  60. // this.titleBar.addEventListener("pointerleave",(e)=>{this.pointerMoveHandler(e);});
  61. // this.titleBar.addEventListener("pointerout",(e)=>{this.pointerMoveHandler(e);});
  62. // this.titleBar.addEventListener("pointerup",(e)=>{this.pointerUpHandler(e);});
  63. this.titleBar.draggable = true;
  64. this.container.appendChild(this.titleBar);
  65. this.body = document.createElement("div");
  66. this.body.className = "floatingWindowBody";
  67. this.windowOpen = GM_getValue(`${this.container.id}_open`,this.settings.open);
  68. if (this.windowOpen) this.container.classList.add("active");
  69. this.body.style.display = this.windowOpen ? this.settings.bodyDisplay : "none";
  70. this.container.appendChild(this.body);
  71. document.body.appendChild(this.container);
  72. this.pointerValues = {
  73. newPosition:null,
  74. positionOffset:null
  75. };
  76. }
  77. toggleWindow() {
  78. this.windowOpen = !this.windowOpen;
  79. this.container.classList.toggle("active");
  80. GM_setValue(`${this.container.id}_open`,this.windowOpen);
  81. this.body.style.display = this.windowOpen ? this.settings.bodyDisplay : "none";
  82. }
  83. dragStartHandler(e) {
  84. this.pointerValues.positionOffset = {
  85. x:e.clientX - this.windowPosition.x,
  86. y:e.clientY - this.windowPosition.y
  87. };
  88. e.dataTransfer.effectAllowed = "move";
  89. }
  90. dragHandler(e) {
  91. if (this.pointerValues.positionOffset) {
  92. this.pointerValues.newPosition = {
  93. x:e.clientX - this.pointerValues.positionOffset.x,
  94. y:e.clientY - this.pointerValues.positionOffset.y
  95. };
  96. }
  97. }
  98. dragEndHandler(e) {
  99. e.preventDefault();
  100. this.pointerValues.newPosition = {
  101. x:e.clientX - this.pointerValues.positionOffset.x,
  102. y:e.clientY - this.pointerValues.positionOffset.y
  103. };
  104. this.container.style.left = `${this.pointerValues.newPosition.x}px`;
  105. this.container.style.top = `${this.pointerValues.newPosition.y}px`;
  106. this.windowPosition = this.pointerValues.newPosition;
  107. GM_setValue(`${this.container.id}_position`,this.windowPosition);
  108. this.pointerValues.dragging = false;
  109. this.pointerValues.positionOffset = this.pointerValues.newPosition = null;
  110. }
  111. }

QingJ © 2025

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