ChatGPT-input-helper

Help organize commonly used spells quickly

目前为 2023-05-08 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name ChatGPT-input-helper
  3. // @name:zh-TW ChatGPT-input-helper 快速輸入常用咒文
  4. // @namespace https://github.com/we684123/ChatGPT-input-helper
  5. // @version 0.0.11
  6. // @author we684123
  7. // @description Help organize commonly used spells quickly
  8. // @description:zh-TW 幫助快速組織常用咒文
  9. // @license MIT
  10. // @icon https://chat.openai.com/favicon.ico
  11. // @match https://chat.openai.com/chat
  12. // @match https://chat.openai.com/chat/*
  13. // @match https://chat.openai.com/chat?*
  14. // @match https://chat.openai.com/?model=*
  15. // @match https://chat.openai.com/c/*
  16. // @match https://chat.openai.com/
  17. // @grant GM_getValue
  18. // @grant GM_setValue
  19. // @grant GM_deleteValue
  20. // @run-at document-end
  21. // ==/UserScript==
  22.  
  23. (function (factory) {
  24. typeof define === 'function' && define.amd ? define(factory) :
  25. factory();
  26. })((function () { 'use strict';
  27.  
  28. const sentinel = (() => {
  29. const isArray = Array.isArray;
  30. let selectorToAnimationMap = {};
  31. let animationCallbacks = {};
  32. let styleEl;
  33. let styleSheet;
  34. let cssRules;
  35. return {
  36. // `on` 方法用於添加 CSS 選擇器的監聽器。
  37. // cssSelectors: 一個字符串或字符串數組,包含要監聽的 CSS 選擇器。
  38. // callback: 用於處理觸發的事件的回調函數。
  39. on: function (cssSelectors, callback) {
  40. // 如果沒有提供回調函數,則直接返回。
  41. if (!callback)
  42. return;
  43. // 如果 `styleEl` 未定義,創建一個新的 `style` 標籤並將其添加到文檔的 `head` 中。
  44. // 還會為 `animationstart` 事件添加事件監聽器。
  45. if (!styleEl) {
  46. const doc = document;
  47. const head = doc.head;
  48. doc.addEventListener("animationstart", function (ev) {
  49. const callbacks = animationCallbacks[ev.animationName];
  50. if (!callbacks)
  51. return;
  52. ev.stopImmediatePropagation();
  53. for (const cb of callbacks) {
  54. cb(ev.target);
  55. }
  56. }, true);
  57. styleEl = doc.createElement("style");
  58. // head.insertBefore(styleEl, head.firstChild); // 這個是原版的,改用下面的
  59. head.append(styleEl); // 感謝 chatgpt-exporter 搞好久 (┬┬﹏┬┬)
  60. styleSheet = styleEl.sheet;
  61. cssRules = styleSheet.cssRules;
  62. }
  63. // 根據提供的選擇器創建一個新的動畫。
  64. const selectors = isArray(cssSelectors) ? cssSelectors : [cssSelectors];
  65. selectors.forEach((selector) => {
  66. // 獲取或創建動畫 ID。
  67. let animIds = selectorToAnimationMap[selector];
  68. if (!animIds) {
  69. const isCustomName = selector[0] == "!";
  70. const animId = isCustomName
  71. ? selector.slice(1)
  72. : "sentinel-" + Math.random().toString(16).slice(2);
  73. // 創建新的 keyframes 規則。
  74. const keyframeRule = cssRules[styleSheet.insertRule("@keyframes " +
  75. animId +
  76. "{from{transform:none;}to{transform:none;}}", cssRules.length)];
  77. keyframeRule._id = selector;
  78. // 如果選擇器不是自定義名稱,則為其創建對應的CSS 規則。
  79. if (!isCustomName) {
  80. const selectorRule = cssRules[styleSheet.insertRule(selector + "{animation-duration:0.0001s;animation-name:" + animId + ";}", cssRules.length)];
  81. selectorRule._id = selector;
  82. }
  83. animIds = [animId];
  84. selectorToAnimationMap[selector] = animIds;
  85. }
  86. // 遍歷動畫 ID,將回調函數添加到動畫回調列表中。
  87. animIds.forEach((animId) => {
  88. animationCallbacks[animId] = animationCallbacks[animId] || [];
  89. animationCallbacks[animId].push(callback);
  90. });
  91. });
  92. },
  93. // `off` 方法用於移除 CSS 選擇器的監聽器。
  94. // cssSelectors: 一個字符串或字符串數組,包含要停止監聽的 CSS 選擇器。
  95. // callback: 可選的回調函數。如果提供,則僅移除與之匹配的監聽器。
  96. off: function (cssSelectors, callback) {
  97. // 將提供的選擇器轉換為數組形式。
  98. const selectors = isArray(cssSelectors) ? cssSelectors : [cssSelectors];
  99. // 遍歷選擇器,移除對應的監聽器。
  100. selectors.forEach((selector) => {
  101. const animIds = selectorToAnimationMap[selector];
  102. if (!animIds)
  103. return;
  104. animIds.forEach((animId) => {
  105. const callbacks = animationCallbacks[animId];
  106. if (!callbacks)
  107. return;
  108. // 如果提供了回調函數,則僅移除與之匹配的監聽器。
  109. if (callback) {
  110. const index = callbacks.indexOf(callback);
  111. if (index !== -1) {
  112. callbacks.splice(index, 1);
  113. }
  114. }
  115. else {
  116. delete animationCallbacks[animId];
  117. }
  118. // 如果該選擇器沒有任何回調函數,則從選擇器映射和 CSS 規則中移除它。
  119. if (callbacks.length === 0) {
  120. delete selectorToAnimationMap[selector];
  121. const rulesToDelete = [];
  122. for (let i = 0, len = cssRules.length; i < len; i++) {
  123. const rule = cssRules[i];
  124. if (rule._id === selector) {
  125. rulesToDelete.push(rule);
  126. }
  127. }
  128. rulesToDelete.forEach((rule) => {
  129. const index = Array.prototype.indexOf.call(cssRules, rule);
  130. if (index !== -1) {
  131. styleSheet.deleteRule(index);
  132. }
  133. });
  134. }
  135. });
  136. });
  137. }
  138. };
  139. })();
  140.  
  141. function onloadSafe(fn) {
  142. if (document.readyState === "complete") {
  143. fn();
  144. }
  145. else {
  146. window.addEventListener("load", fn);
  147. }
  148. }
  149.  
  150. function styleInject(css, ref) {
  151. if ( ref === void 0 ) ref = {};
  152. var insertAt = ref.insertAt;
  153.  
  154. if (!css || typeof document === 'undefined') { return; }
  155.  
  156. var head = document.head || document.getElementsByTagName('head')[0];
  157. var style = document.createElement('style');
  158. style.type = 'text/css';
  159.  
  160. if (insertAt === 'top') {
  161. if (head.firstChild) {
  162. head.insertBefore(style, head.firstChild);
  163. } else {
  164. head.appendChild(style);
  165. }
  166. } else {
  167. head.appendChild(style);
  168. }
  169.  
  170. if (style.styleSheet) {
  171. style.styleSheet.cssText = css;
  172. } else {
  173. style.appendChild(document.createTextNode(css));
  174. }
  175. }
  176.  
  177. var css_248z$2 = ".buttonStyles-module_container__l-r9Y{align-items:center;border:1px solid #fff;border-radius:5px;box-sizing:border-box;display:flex;justify-content:center;position:relative;width:100%}.buttonStyles-module_mainButton__b08pW{border:1px solid #fff;border-radius:5px;margin:0 auto;padding:8px 12px;width:85%}.buttonStyles-module_mainButton__b08pW,.buttonStyles-module_settingButton__-opQi{background-color:#202123;box-sizing:border-box;color:#fff;cursor:pointer;font-size:14px}.buttonStyles-module_settingButton__-opQi{border:none;border-radius:5px;padding:8px 14px;width:15%}.buttonStyles-module_menu__aeYDY{background-color:#202123;border:1px solid #fff;border-radius:15px;display:none;left:0;max-height:240px;overflow-y:auto;position:absolute;top:0;width:100%;z-index:9999}.buttonStyles-module_menuButton__eg9D8{background-color:#202123;border:1px solid #fff;border-radius:5px;color:#fff;cursor:pointer;display:block;font-size:14px;height:100%;padding:8px 12px;width:100%}.buttonStyles-module_containerNode_class__1rDgQ{position:relative}";
  178. var styles$2 = {"container":"buttonStyles-module_container__l-r9Y","mainButton":"buttonStyles-module_mainButton__b08pW","settingButton":"buttonStyles-module_settingButton__-opQi","menu":"buttonStyles-module_menu__aeYDY","menuButton":"buttonStyles-module_menuButton__eg9D8","containerNode_class":"buttonStyles-module_containerNode_class__1rDgQ"};
  179. styleInject(css_248z$2);
  180.  
  181. // library.ts
  182. const config = {
  183. name: "aims-helper",
  184. init_customize: [
  185. {
  186. name: '繁體中文初始化',
  187. position: 'start',
  188. autoEnter: true,
  189. content: [
  190. `以下問答請使用繁體中文,並使用台灣用語。\n`,
  191. ].join("")
  192. }, {
  193. name: '請繼續',
  194. position: 'start',
  195. autoEnter: true,
  196. content: [
  197. `請繼續`,
  198. ].join("")
  199. }, {
  200. name: '請從""繼續',
  201. position: 'start',
  202. autoEnter: false,
  203. content: [
  204. `請從""繼續`,
  205. ].join("")
  206. }
  207. ],
  208. // ↓ 左邊選單的定位(上層)
  209. NAV_MENU: 'nav > div.overflow-y-auto',
  210. // ↓ 輸入框的定位
  211. TEXT_INPUTBOX_POSITION: 'textarea.m-0',
  212. // ↓ 送出按鈕的定位
  213. SUBMIT_BUTTON_POSITION: 'button.absolute',
  214. // ↓ 選單按鈕
  215. MAIN_BUTTON_CLASS: 'main_button',
  216. // ↓ 控制按鈕
  217. SETTING_BUTTON_CLASS: 'setting_button',
  218. // ↓ 選單
  219. MENU_CLASS: 'main_menu',
  220. // ↓ 按鈕文字
  221. HELPER_MENU_TEXT: 'input helper',
  222. // ↓ 按鈕用容器
  223. CONTAINER_CLASS: 'helper_textcontainer',
  224. // ↓ 模擬輸入於輸入框的事件
  225. INPUT_EVENT: new Event('input', { bubbles: true }),
  226. };
  227.  
  228. // 將自定義內容插入到輸入框中
  229. const insertCustomize = (customize, name) => {
  230. const textInputbox = document.querySelector(config.TEXT_INPUTBOX_POSITION);
  231. const item = customize.find((i) => i.name === name);
  232. if (item) {
  233. if (item.position === 'start') {
  234. textInputbox.value = item.content + textInputbox.value;
  235. }
  236. else {
  237. textInputbox.value += item.content;
  238. }
  239. textInputbox.dispatchEvent(config.INPUT_EVENT);
  240. textInputbox.focus();
  241. if (item.autoEnter) {
  242. setTimeout(() => {
  243. const submitButton = document.querySelector(config.SUBMIT_BUTTON_POSITION);
  244. submitButton.click();
  245. }, 100);
  246. }
  247. }
  248. else {
  249. console.error(`找不到名稱為 ${name} 的元素`);
  250. }
  251. };
  252.  
  253. // 創造主按鈕
  254. const createMainButton = (buttonText) => {
  255. const mainButton = document.createElement("button");
  256. mainButton.innerText = buttonText;
  257. mainButton.classList.add(styles$2.mainButton);
  258. mainButton.style.width = "86%";
  259. return mainButton;
  260. };
  261. // 創造設定按鈕
  262. const createSettingButton = () => {
  263. const settingButton = document.createElement("button");
  264. settingButton.innerText = "⚙️";
  265. settingButton.classList.add(styles$2.settingButton);
  266. settingButton.style.width = "14%";
  267. settingButton.id = "settingButton";
  268. return settingButton;
  269. };
  270. // 創造選項
  271. const createMenuItem = (element, customize) => {
  272. const menuItem = document.createElement("button");
  273. menuItem.innerText = element.name;
  274. menuItem.id = element.name;
  275. menuItem.classList.add(styles$2.menuButton);
  276. menuItem.addEventListener("click", (event) => {
  277. insertCustomize(customize, event.target.id);
  278. });
  279. return menuItem;
  280. };
  281. // 創造選單(包含多個選項)
  282. const createMenu = (containerNode, customize) => {
  283. // 創造選單
  284. const menu = document.createElement("div");
  285. menu.id = "helper_menu";
  286. menu.classList.add(styles$2.menu);
  287. menu.style.display = "none";
  288. menu.style.width = `${containerNode.offsetWidth}px`;
  289. // 創造選項
  290. customize.forEach((element) => {
  291. const menuItem = createMenuItem(element, customize);
  292. menu.appendChild(menuItem);
  293. });
  294. // 設定選單的高度
  295. const windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  296. const customizeUnitHeight = 39;
  297. const menuMaxHeight = windowHeight - customizeUnitHeight * 2;
  298. const MaxCustomizeLen = Number(menuMaxHeight / customizeUnitHeight);
  299. let customizeLen = customize.length > MaxCustomizeLen ? MaxCustomizeLen : customize.length;
  300. console.log(`customize.length = ${customize.length}`);
  301. console.log(`CustomizeLen = ${customizeLen}`);
  302. console.log(`menuMaxHeight = ${menuMaxHeight}`);
  303. if (customizeLen > 2) {
  304. let offset = (customizeLen - 2) * customizeUnitHeight;
  305. menu.style.top = `-${offset}px`;
  306. console.log(`offset = ${offset}`);
  307. }
  308. // 設定選單最大高度
  309. menu.style.maxHeight = `${menuMaxHeight}px`;
  310. return menu;
  311. };
  312.  
  313. const bindElementContainer = (elements, containerClass) => {
  314. const container = document.createElement("div");
  315. if (containerClass) {
  316. container.classList.add(containerClass);
  317. }
  318. elements.forEach((element) => {
  319. container.appendChild(element);
  320. });
  321. return container;
  322. };
  323.  
  324. // addMenuBtn 函數用於新增包含主按鈕和設定按鈕的選單按鈕
  325. function addMenuBtnWrapper(containerNode, customize, buttonText = "Click Me" // 主按鈕的文字,預設值為 "Click Me"
  326. ) {
  327. // 創建主按鈕和設定按鈕
  328. const mainButton = createMainButton(buttonText);
  329. const settingButton = createSettingButton();
  330. // 將主按鈕和設定按鈕組合在一個容器中
  331. const assButton = bindElementContainer([settingButton, mainButton], config.CONTAINER_CLASS);
  332. // 根據客製化選單項目創建選單
  333. const menu = createMenu(containerNode, customize);
  334. // 當滑鼠移到按鈕上時,顯示選單
  335. assButton.addEventListener("mouseenter", () => {
  336. menu.style.display = "block";
  337. });
  338. // 創建按鈕包裹器,並將組合按鈕和選單加入其中
  339. const buttonWrapper = document.createElement("div");
  340. buttonWrapper.style.width = `${containerNode.offsetWidth}px`;
  341. buttonWrapper.appendChild(assButton);
  342. buttonWrapper.appendChild(menu);
  343. // 將按鈕包裹器加入到容器節點中
  344. containerNode.appendChild(buttonWrapper);
  345. // 當滑鼠離開按鈕包裹器時,隱藏選單
  346. buttonWrapper.addEventListener("mouseleave", () => {
  347. setTimeout(() => {
  348. menu.style.display = "none";
  349. }, 300);
  350. });
  351. console.log("已新增按鈕");
  352. }
  353.  
  354. var css_248z$1 = ".formPopupStyles-module_form-popup__cpX-x{background-color:#40414f;border:1px solid #000;height:60%;left:50%;max-height:1200px;max-width:800px;padding:30px;position:fixed;top:50%;transform:translate(-50%,-50%);width:80%;z-index:9999}.formPopupStyles-module_form__A8xi3{display:flex;flex-direction:column;gap:15px}.formPopupStyles-module_form-row__sMrG8{display:flex;flex-direction:column;gap:5px}.formPopupStyles-module_input__f-v3V{background-color:#545766;border:1px solid #fff;color:#fff;margin-left:4px;padding:4px 8px}textarea.formPopupStyles-module_input__f-v3V{min-height:100px;width:100%}";
  355. var styles$1 = {"form-popup":"formPopupStyles-module_form-popup__cpX-x","form":"formPopupStyles-module_form__A8xi3","form-row":"formPopupStyles-module_form-row__sMrG8","input":"formPopupStyles-module_input__f-v3V"};
  356. styleInject(css_248z$1);
  357.  
  358. // createFormPopup.ts
  359. function createFormPopup(options) {
  360. // 創建彈出視窗
  361. const formPopup = document.createElement('div');
  362. formPopup.className = styles$1['form-popup'];
  363. // 創建標題
  364. const titleLabel = document.createElement('h2');
  365. titleLabel.textContent = options.title;
  366. formPopup.appendChild(titleLabel);
  367. // 創建表單
  368. const form = document.createElement('form');
  369. formPopup.appendChild(form);
  370. form.className = styles$1.form;
  371. // 創建名稱輸入框
  372. const nameLabel = document.createElement('label');
  373. nameLabel.textContent = '名稱(name)';
  374. form.appendChild(nameLabel);
  375. const nameInput = document.createElement('input');
  376. nameInput.type = 'text';
  377. nameInput.className = styles$1.input;
  378. form.appendChild(nameInput);
  379. // 創建位置選擇
  380. const positionLabel = document.createElement('label');
  381. positionLabel.textContent = '位置(position)';
  382. form.appendChild(positionLabel);
  383. const positionSelect = document.createElement('select');
  384. positionSelect.className = styles$1.input;
  385. const positionStartOption = document.createElement('option');
  386. positionStartOption.value = 'start';
  387. positionStartOption.textContent = 'start';
  388. const positionEndOption = document.createElement('option');
  389. positionEndOption.value = 'end';
  390. positionEndOption.textContent = 'end';
  391. positionSelect.appendChild(positionStartOption);
  392. positionSelect.appendChild(positionEndOption);
  393. form.appendChild(positionSelect);
  394. // 創建是否自動輸入選擇
  395. const autoEnterLabel = document.createElement('label');
  396. autoEnterLabel.textContent = '是否自動輸入(AutoEnter)';
  397. form.appendChild(autoEnterLabel);
  398. const autoEnterInput = document.createElement('input');
  399. autoEnterInput.type = 'checkbox';
  400. form.appendChild(autoEnterInput);
  401. // 創建內容輸入框
  402. const contentLabel = document.createElement('label');
  403. contentLabel.textContent = '內容(content)';
  404. form.appendChild(contentLabel);
  405. const contentTextarea = document.createElement('textarea');
  406. contentTextarea.className = `${styles$1.input} ${styles$1['textarea-input']}`;
  407. form.appendChild(contentTextarea);
  408. // 創建提交按鈕
  409. const submitButton = document.createElement('button');
  410. submitButton.type = 'submit';
  411. submitButton.textContent = '提交';
  412. form.appendChild(submitButton);
  413. // 根據編輯模式,填充初始值
  414. if (options.mode === 'edit' && options.initialValues) {
  415. nameInput.value = options.initialValues.name;
  416. positionSelect.value = options.initialValues.position;
  417. autoEnterInput.checked = options.initialValues.autoEnter;
  418. contentTextarea.value = options.initialValues.content;
  419. }
  420. // 提交表單時的處理
  421. form.addEventListener('submit', (event) => {
  422. event.preventDefault();
  423. const values = {
  424. name: nameInput.value,
  425. position: positionSelect.value,
  426. autoEnter: autoEnterInput.checked,
  427. content: contentTextarea.value,
  428. };
  429. console.log('values', values);
  430. options.onSubmit(values);
  431. document.body.removeChild(formPopup);
  432. });
  433. // 點擊彈窗外的地方關閉彈窗
  434. formPopup.addEventListener('click', (event) => {
  435. if (event.target === formPopup) {
  436. document.body.removeChild(formPopup);
  437. }
  438. });
  439. // 將彈出視窗加入頁面中
  440. document.body.appendChild(formPopup);
  441. }
  442.  
  443. var css_248z = ".setCustomizeBtn-module_popup__uF6hF{background:#525467;border:1px solid #000;height:80%;left:50%;max-height:1200px;max-width:800px;padding:30px;position:fixed;top:50%;transform:translate(-50%,-50%);width:80%;z-index:9999}.setCustomizeBtn-module_add-button__IASCv,.setCustomizeBtn-module_delete-button__8I8BH,.setCustomizeBtn-module_edit-button__NqnT6{border:2px solid #fff;margin:10px}.setCustomizeBtn-module_close-button__uw4Q6{position:absolute;right:5px;top:5px}.setCustomizeBtn-module_table-wrapper__LY27P{margin-bottom:20px;max-height:612px;overflow-y:auto}";
  444. var styles = {"popup":"setCustomizeBtn-module_popup__uF6hF","add-button":"setCustomizeBtn-module_add-button__IASCv","edit-button":"setCustomizeBtn-module_edit-button__NqnT6","delete-button":"setCustomizeBtn-module_delete-button__8I8BH","close-button":"setCustomizeBtn-module_close-button__uw4Q6","table-wrapper":"setCustomizeBtn-module_table-wrapper__LY27P"};
  445. styleInject(css_248z);
  446.  
  447. function setCustomizeBtn(customize) {
  448. // 找到 settingButton 元素
  449. const settingButton = document.getElementById('settingButton');
  450. // 當點擊 settingButton 時觸發事件
  451. settingButton.addEventListener('click', () => {
  452. // 創建彈出視窗
  453. const popup = document.createElement('div');
  454. popup.classList.add(styles.popup);
  455. // 創建新增按鈕
  456. const addButton = document.createElement('button');
  457. addButton.textContent = '新增(add)';
  458. addButton.classList.add(styles['add-button']);
  459. // 當點擊 addButton 時觸發事件
  460. addButton.addEventListener('click', () => {
  461. // 使用 createFormPopup 函數
  462. createFormPopup({
  463. title: '新增',
  464. mode: 'add',
  465. onSubmit: (values) => {
  466. const newItem = {
  467. name: values.name,
  468. position: values.position,
  469. autoEnter: values.autoEnter,
  470. content: values.content,
  471. };
  472. customize.push(newItem);
  473. renderTable();
  474. },
  475. });
  476. });
  477. popup.appendChild(addButton);
  478. // 創建編輯按鈕
  479. const editButton = document.createElement('button');
  480. editButton.textContent = '編輯(edit)';
  481. editButton.classList.add(styles['edit-button']);
  482. editButton.addEventListener('click', () => {
  483. // 編輯一個 item
  484. const index = prompt('請輸入要編輯的編號(edit index)');
  485. if (index && Number(index) >= 1 && index <= customize.length) {
  486. const item = customize[Number(index) - 1];
  487. createFormPopup({
  488. title: '編輯',
  489. mode: 'edit',
  490. initialValues: {
  491. name: item.name,
  492. position: item.position,
  493. autoEnter: item.autoEnter,
  494. content: item.content,
  495. },
  496. onSubmit: (newValues) => {
  497. item.name = newValues.name;
  498. item.position = newValues.position;
  499. item.autoEnter = newValues.autoEnter;
  500. item.content = newValues.content;
  501. // 重新渲染表格
  502. renderTable();
  503. },
  504. });
  505. }
  506. else {
  507. alert('輸入的編號不合法');
  508. }
  509. });
  510. popup.appendChild(editButton);
  511. // 創建刪除按鈕
  512. const deleteButton = document.createElement('button');
  513. deleteButton.textContent = '刪除(delete)';
  514. deleteButton.classList.add(styles['delete-button']);
  515. deleteButton.addEventListener('click', () => {
  516. // 刪除一個 item
  517. const index = prompt('請輸入要刪除的編號(delete index)');
  518. if (index && Number(index) >= 1 && index <= customize.length) {
  519. customize.splice(Number(index) - 1, 1);
  520. renderTable();
  521. }
  522. else {
  523. alert('輸入的編號不合法 (invalid index)');
  524. }
  525. });
  526. popup.appendChild(deleteButton);
  527. // 創建關閉按鈕
  528. const closeButton = document.createElement('button');
  529. closeButton.textContent = '儲存並離開(save&exit)';
  530. closeButton.classList.add(styles['close-button']);
  531. closeButton.addEventListener('click', () => {
  532. console.log(customize);
  533. // 儲存修改後的 customize 資料
  534. GM_setValue('customizeData', customize);
  535. // // 重寫一次 helper_menu
  536. // const helper_menu = document.getElementById('helper_menu');
  537. // const menu = createMenu(helper_menu);
  538. // helper_menu.replaceWith(menu);
  539. // 上面的做不出來
  540. // 所以只好重新整理頁面
  541. location.reload();
  542. document.body.removeChild(popup);
  543. });
  544. popup.appendChild(closeButton);
  545. // 創建表格
  546. const table = document.createElement('table');
  547. const tableWrapper = document.createElement('div');
  548. tableWrapper.classList.add(styles['table-wrapper']);
  549. tableWrapper.appendChild(table);
  550. popup.appendChild(tableWrapper);
  551. // 創建表頭
  552. const thead = document.createElement('thead');
  553. const tr = document.createElement('tr');
  554. const th1 = document.createElement('th');
  555. const th2 = document.createElement('th');
  556. const th3 = document.createElement('th');
  557. const th4 = document.createElement('th');
  558. const th5 = document.createElement('th');
  559. th1.textContent = '編號(index)';
  560. th2.textContent = '名稱(name)';
  561. th3.textContent = '位置(position)';
  562. th4.textContent = '自動輸入(autoEnter)?';
  563. th5.textContent = '內容(content)';
  564. tr.appendChild(th1);
  565. tr.appendChild(th2);
  566. tr.appendChild(th3);
  567. tr.appendChild(th4);
  568. tr.appendChild(th5);
  569. thead.appendChild(tr);
  570. table.appendChild(thead);
  571. // 創建表身
  572. const tbody = document.createElement('tbody');
  573. table.appendChild(tbody);
  574. // 渲染表格
  575. function renderTable() {
  576. // 先清空表格內容
  577. tbody.innerHTML = '';
  578. // 重新渲染表格
  579. customize.forEach((item, index) => {
  580. const tr = document.createElement('tr');
  581. const td1 = document.createElement('td');
  582. const td2 = document.createElement('td');
  583. const td3 = document.createElement('td');
  584. const td4 = document.createElement('td');
  585. const td5 = document.createElement('td');
  586. td1.textContent = index + 1;
  587. td2.textContent = item.name;
  588. td3.textContent = item.position;
  589. td4.textContent = item.autoEnter;
  590. td5.textContent = item.content;
  591. tr.appendChild(td1);
  592. tr.appendChild(td2);
  593. tr.appendChild(td3);
  594. tr.appendChild(td4);
  595. tr.appendChild(td5);
  596. tbody.appendChild(tr);
  597. });
  598. }
  599. // 渲染初始表格
  600. renderTable();
  601. // 點擊彈窗外的地方關閉彈窗
  602. popup.addEventListener('click', (event) => {
  603. if (event.target === popup) {
  604. document.body.removeChild(popup);
  605. }
  606. });
  607. // 將彈出視窗加入頁面中
  608. document.body.appendChild(popup);
  609. });
  610. }
  611.  
  612. main();
  613. function main() {
  614. // 頁面載入完成後執行
  615. onloadSafe(() => {
  616. // 監聽 nav 元素
  617. console.log("=====監聽 nav 元素=====");
  618. // 定義常用咒文
  619. let customize;
  620. sentinel.on("nav", (nav) => {
  621. console.log("===== trigger sentinel.on nav =====");
  622. // 讀取 customize 設定
  623. let GM_customize = GM_getValue("customizeData", customize);
  624. // 如果 user 已經有設定了就用 user 的,沒有就用預設值
  625. if (GM_customize) {
  626. customize = GM_customize;
  627. }
  628. else {
  629. customize = config.init_customize;
  630. GM_setValue("customizeData", customize);
  631. }
  632. //找不到就新增
  633. const container = document.getElementById("helper_menu");
  634. if (!container) {
  635. // 獲得目標元素
  636. const aimsNode = document.querySelector(config.NAV_MENU);
  637. // 新增一個容器
  638. const container = document.createElement("div");
  639. container.classList.add(styles$2.containerNode_class);
  640. container.id = "helper_menu";
  641. if (aimsNode) {
  642. // 設定 container 寬度為父元素寬度
  643. container.style.width = `${aimsNode.offsetWidth}px`; // 設定 container 寬度為父元素寬度
  644. // 將容器元素插入到目標元素後面
  645. aimsNode.parentNode?.insertBefore(container, aimsNode.nextSibling);
  646. // 新增一個按鈕元素
  647. addMenuBtnWrapper(container, customize, config.HELPER_MENU_TEXT);
  648. // 設定 "設定按鈕"的點擊事件
  649. setCustomizeBtn(customize);
  650. }
  651. }
  652. });
  653. });
  654. }
  655.  
  656. }));

QingJ © 2025

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