application

本地存储预览

当前为 2024-09-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name application
  3. // @namespace npm/vite-plugin-monkey
  4. // @version 1.0.6
  5. // @author monkey
  6. // @description 本地存储预览
  7. // @license MIT
  8. // @icon https://www.qianxin.com/favicon.ico
  9. // @match *://*/*
  10. // @grant GM_setClipboard
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. class Application {
  16. constructor() {
  17. this.list = [];
  18. this.loading = false;
  19. }
  20. setHtml() {
  21. const container = document.createElement("div");
  22. container.className = "youhou-container";
  23. container.style.cssText = "position:fixed;bottom:20px;right:20px;";
  24. const html = '<div class="youhou-box">查看数据</div>';
  25. container.innerHTML = html;
  26. document.body.appendChild(container);
  27. }
  28. setStyle() {
  29. const style = `
  30. .youhou-box {
  31. width: fit-content;
  32. font-size: 12px;
  33. color: #fff;
  34. background:#000;
  35. padding: 5px 10px;
  36. border-radius: 5px;
  37. cursor: pointer;
  38. }
  39. `;
  40. const styleTag = document.createElement("style");
  41. styleTag.innerHTML = style;
  42. document.head.appendChild(styleTag);
  43. }
  44. showTip(x, y) {
  45. const div = document.createElement("div");
  46. div.style.cssText =
  47. "position:fixed;top:20px;z-index:9999;text-align:center;color:#fff;background:#000;width:fit-content;padding:5px 10px;border-radius:5px;";
  48. div.style.left = x + "px";
  49. div.style.top = y + "px";
  50. document.body.appendChild(div);
  51. div.innerText = "复制成功";
  52. const moveAndChangeColor = [
  53. {
  54. transform: "translateY(0)",
  55. },
  56. {
  57. offset: 0.6,
  58. transform: "translateY(-60px)",
  59. },
  60. {
  61. transform: "translateY(-100px)",
  62. },
  63. ];
  64. const animation = div.animate(moveAndChangeColor, {
  65. duration: 1000,
  66. fill: "forwards",
  67. easing: "ease-in-out",
  68. });
  69. setTimeout(() => {
  70. document.body.removeChild(div);
  71. this.loading = false;
  72. }, 1000);
  73. }
  74.  
  75. setEvent() {
  76. const container = document.querySelector(".youhou-container");
  77. const box = document.querySelector(".youhou-box");
  78. box.addEventListener("dblclick", (e) => {
  79. const div = document.createElement("div");
  80. const _this = this;
  81. const trs = this.list
  82. .map((v) => {
  83. return `
  84. <tr style="border-bottom:1px solid #f5f5f5;padding:10px;" class="youhou-box-tr">
  85. <td>${v.type}</td>
  86. <td>${v.key}</td>
  87. <td style="position:relative;text-align:center;display:block;border:none;">
  88. <div style="position:absolute;white-space:nowrap;width:-webkit-fill-available;overflow:hidden;text-overflow:ellipsis;" title="${v.value}">
  89. ${v.value}
  90. </div>
  91. </td>
  92. <td>
  93. <div class="youhou-box-copy" data-value="${v.value}" onclick="navigator.clipboard.writeText('${v.value}');" style="color:#fff;background:#000;cursor:pointer;border-radius:5px;padding:5px 10px;">复制</div>
  94. </td>
  95. </tr>
  96. `;
  97. })
  98. .join("");
  99. div.innerHTML = `
  100. <div style="width:1200px;height:500px;background:#fff;padding:10px;overflow:auto" class="youhou-box-table">
  101. <table id="table111" align="center" cellspacing="0" cellpadding="10" style="table-layout:auto;border-collapse: collapse;text-align:center;width:100%;">
  102. <tr style="border-bottom:1px solid #f5f5f5;padding:10px;">
  103. <th width="100">类型</th>
  104. <th width="100">键</th>
  105. <th>值</th>
  106. <th width="100">操作</th>
  107. </tr>
  108. ${trs}
  109. </table>
  110. </div>
  111. `;
  112. div.style.cssText =
  113. "display:flex;justify-content:center;align-items:center;position:fixed;z-index:9999;top:0;left:0;width:100%;height:100%;background-color:rgba(0,0,0,0.5);";
  114. div.onclick = () => {
  115. this.loading = false;
  116. document.body.removeChild(div);
  117. };
  118. document.body.appendChild(div);
  119. div
  120. .querySelector(".youhou-box-table")
  121. .addEventListener("click", (e) => {
  122. e.stopPropagation();
  123. if (e.target.className === "youhou-box-copy" && !this.loading) {
  124. navigator.clipboard.writeText(e.target.dataset.value);
  125. const x = e.clientX;
  126. const y = e.clientY;
  127. console.log(x, y);
  128. this.loading = true;
  129. this.showTip(x, y);
  130. }
  131. });
  132. });
  133. this.setMove(container);
  134. }
  135. setMove(draggableElement) {
  136. let isDragging = false;
  137. let offsetX, offsetY;
  138. let w = document.documentElement.clientWidth;
  139. let h = document.documentElement.clientHeight;
  140. let elementRect = draggableElement.getBoundingClientRect();
  141. draggableElement.addEventListener("mousedown", startDragging);
  142. document.addEventListener("mousemove", drag);
  143. document.addEventListener("mouseup", stopDragging);
  144.  
  145. function startDragging(event) {
  146. console.log("vent.target: ", event.target.className);
  147. if (event.target.className == "youhou-box") {
  148. isDragging = true;
  149. offsetX = event.clientX - draggableElement.offsetLeft;
  150. offsetY = event.clientY - draggableElement.offsetTop;
  151. event.preventDefault();
  152. }
  153. }
  154.  
  155. function drag(event) {
  156. if (isDragging) {
  157. let x = event.clientX - offsetX;
  158. let y = event.clientY - offsetY;
  159.  
  160. // 限制在容器范围内
  161. x = Math.max(0, Math.min(x, w - elementRect.width));
  162. y = Math.max(0, Math.min(y, h - elementRect.height));
  163.  
  164. draggableElement.style.left = x + "px";
  165. draggableElement.style.top = y + "px";
  166. }
  167. }
  168.  
  169. function stopDragging() {
  170. isDragging = false;
  171. }
  172. }
  173. setData() {
  174. this.list = [];
  175. this.list.push({
  176. type: "cookie",
  177. key: "cookie",
  178. value: document.cookie,
  179. });
  180. Object.keys(localStorage).forEach((key) => {
  181. this.list.push({
  182. type: "localStorage",
  183. key: key,
  184. value: localStorage.getItem(key),
  185. });
  186. });
  187. Object.keys(sessionStorage).forEach((key) => {
  188. this.list.push({
  189. type: "sessionStorage",
  190. key: key,
  191. value: sessionStorage.getItem(key),
  192. });
  193. });
  194. }
  195. render() {
  196. this.setHtml();
  197. this.setStyle();
  198. this.setEvent();
  199. this.setData();
  200. console.log(this.list);
  201. window.onresize = () => {
  202. this.setMove1();
  203. };
  204. }
  205. }
  206. const app = new Application();
  207. app.render();
  208. })();