application

本地存储预览

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