application

本地存储预览

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

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