ChatGPT-input-helper

Help organize commonly used spells quickly

当前为 2023-04-23 提交的版本,查看 最新版本

  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.10
  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:100%;max-height:240px;overflow-y:auto;position:absolute;top:0;width:100%;z-index:1}.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 menuHeight: number = menu.offsetHeight;
  297. const customizeUnitHeight = 39;
  298. const menuMaxHeight = windowHeight - customizeUnitHeight * 2;
  299. const MaxCustomizeLen = Number(menuMaxHeight / customizeUnitHeight);
  300. let customizeLen = customize.length > MaxCustomizeLen ? MaxCustomizeLen : customize.length;
  301. console.log(`customize.length = ${customize.length}`);
  302. console.log(`CutomizeLen = ${customizeLen}`);
  303. console.log(`menuMaxHeight = ${menuMaxHeight}`);
  304. if (customizeLen > 2) {
  305. let offset = (customizeLen - 2) * customizeUnitHeight;
  306. menu.style.top = `-${offset}px`;
  307. console.log(`offset = ${offset}`);
  308. }
  309. // 設定選單最大高度
  310. menu.style.maxHeight = `${menuMaxHeight}px`;
  311. return menu;
  312. };
  313.  
  314. const bindElementContainer = (elements, containerClass) => {
  315. const container = document.createElement("div");
  316. if (containerClass) {
  317. container.classList.add(containerClass);
  318. }
  319. elements.forEach((element) => {
  320. container.appendChild(element);
  321. });
  322. return container;
  323. };
  324.  
  325. // addMenuBtn 函數用於新增包含主按鈕和設定按鈕的選單按鈕
  326. function addMenuBtnWrapper(containerNode, customize, buttonText = "Click Me" // 主按鈕的文字,預設值為 "Click Me"
  327. ) {
  328. // 創建主按鈕和設定按鈕
  329. const mainButton = createMainButton(buttonText);
  330. const settingButton = createSettingButton();
  331. // 將主按鈕和設定按鈕組合在一個容器中
  332. const assButton = bindElementContainer([settingButton, mainButton], config.CONTAINER_CLASS);
  333. // 根據客製化選單項目創建選單
  334. const menu = createMenu(containerNode, customize);
  335. // 當滑鼠移到按鈕上時,顯示選單
  336. assButton.addEventListener("mouseenter", () => {
  337. menu.style.display = "block";
  338. });
  339. // 創建按鈕包裹器,並將組合按鈕和選單加入其中
  340. const buttonWrapper = document.createElement("div");
  341. buttonWrapper.style.width = `${containerNode.offsetWidth}px`;
  342. buttonWrapper.appendChild(assButton);
  343. buttonWrapper.appendChild(menu);
  344. // 將按鈕包裹器加入到容器節點中
  345. containerNode.appendChild(buttonWrapper);
  346. // 當滑鼠離開按鈕包裹器時,隱藏選單
  347. buttonWrapper.addEventListener("mouseleave", () => {
  348. setTimeout(() => {
  349. menu.style.display = "none";
  350. }, 300);
  351. });
  352. console.log("已新增按鈕");
  353. }
  354.  
  355. 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%}";
  356. 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"};
  357. styleInject(css_248z$1);
  358.  
  359. // createFormPopup.ts
  360. function createFormPopup(options) {
  361. // 創建彈出視窗
  362. const formPopup = document.createElement('div');
  363. formPopup.className = styles$1['form-popup'];
  364. // 創建標題
  365. const titleLabel = document.createElement('h2');
  366. titleLabel.textContent = options.title;
  367. formPopup.appendChild(titleLabel);
  368. // 創建表單
  369. const form = document.createElement('form');
  370. formPopup.appendChild(form);
  371. form.className = styles$1.form;
  372. // 創建名稱輸入框
  373. const nameLabel = document.createElement('label');
  374. nameLabel.textContent = '名稱(name)';
  375. form.appendChild(nameLabel);
  376. const nameInput = document.createElement('input');
  377. nameInput.type = 'text';
  378. nameInput.className = styles$1.input;
  379. form.appendChild(nameInput);
  380. // 創建位置選擇
  381. const positionLabel = document.createElement('label');
  382. positionLabel.textContent = '位置(position)';
  383. form.appendChild(positionLabel);
  384. const positionSelect = document.createElement('select');
  385. positionSelect.className = styles$1.input;
  386. const positionStartOption = document.createElement('option');
  387. positionStartOption.value = 'start';
  388. positionStartOption.textContent = 'start';
  389. const positionEndOption = document.createElement('option');
  390. positionEndOption.value = 'end';
  391. positionEndOption.textContent = 'end';
  392. positionSelect.appendChild(positionStartOption);
  393. positionSelect.appendChild(positionEndOption);
  394. form.appendChild(positionSelect);
  395. // 創建是否自動輸入選擇
  396. const autoEnterLabel = document.createElement('label');
  397. autoEnterLabel.textContent = '是否自動輸入(AutoEnter)';
  398. form.appendChild(autoEnterLabel);
  399. const autoEnterInput = document.createElement('input');
  400. autoEnterInput.type = 'checkbox';
  401. form.appendChild(autoEnterInput);
  402. // 創建內容輸入框
  403. const contentLabel = document.createElement('label');
  404. contentLabel.textContent = '內容(content)';
  405. form.appendChild(contentLabel);
  406. const contentTextarea = document.createElement('textarea');
  407. contentTextarea.className = `${styles$1.input} ${styles$1['textarea-input']}`;
  408. form.appendChild(contentTextarea);
  409. // 創建提交按鈕
  410. const submitButton = document.createElement('button');
  411. submitButton.type = 'submit';
  412. submitButton.textContent = '提交';
  413. form.appendChild(submitButton);
  414. // 根據編輯模式,填充初始值
  415. if (options.mode === 'edit' && options.initialValues) {
  416. nameInput.value = options.initialValues.name;
  417. positionSelect.value = options.initialValues.position;
  418. autoEnterInput.checked = options.initialValues.autoEnter;
  419. contentTextarea.value = options.initialValues.content;
  420. }
  421. // 提交表單時的處理
  422. form.addEventListener('submit', (event) => {
  423. event.preventDefault();
  424. const values = {
  425. name: nameInput.value,
  426. position: positionSelect.value,
  427. autoEnter: autoEnterInput.checked,
  428. content: contentTextarea.value,
  429. };
  430. console.log('values', values);
  431. options.onSubmit(values);
  432. document.body.removeChild(formPopup);
  433. });
  434. // 點擊彈窗外的地方關閉彈窗
  435. formPopup.addEventListener('click', (event) => {
  436. if (event.target === formPopup) {
  437. document.body.removeChild(formPopup);
  438. }
  439. });
  440. // 將彈出視窗加入頁面中
  441. document.body.appendChild(formPopup);
  442. }
  443.  
  444. 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}";
  445. 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"};
  446. styleInject(css_248z);
  447.  
  448. function setCustomizeBtn(customize) {
  449. // 找到 settingButton 元素
  450. const settingButton = document.getElementById('settingButton');
  451. // 當點擊 settingButton 時觸發事件
  452. settingButton.addEventListener('click', () => {
  453. // 創建彈出視窗
  454. const popup = document.createElement('div');
  455. popup.classList.add(styles.popup);
  456. // 創建新增按鈕
  457. const addButton = document.createElement('button');
  458. addButton.textContent = '新增(add)';
  459. addButton.classList.add(styles['add-button']);
  460. // 當點擊 addButton 時觸發事件
  461. addButton.addEventListener('click', () => {
  462. // 使用 createFormPopup 函數
  463. createFormPopup({
  464. title: '新增',
  465. mode: 'add',
  466. onSubmit: (values) => {
  467. const newItem = {
  468. name: values.name,
  469. position: values.position,
  470. autoEnter: values.autoEnter,
  471. content: values.content,
  472. };
  473. customize.push(newItem);
  474. renderTable();
  475. },
  476. });
  477. });
  478. popup.appendChild(addButton);
  479. // 創建編輯按鈕
  480. const editButton = document.createElement('button');
  481. editButton.textContent = '編輯(edit)';
  482. editButton.classList.add(styles['edit-button']);
  483. editButton.addEventListener('click', () => {
  484. // 編輯一個 item
  485. const index = prompt('請輸入要編輯的編號(edit index)');
  486. if (index && Number(index) >= 1 && index <= customize.length) {
  487. const item = customize[Number(index) - 1];
  488. createFormPopup({
  489. title: '編輯',
  490. mode: 'edit',
  491. initialValues: {
  492. name: item.name,
  493. position: item.position,
  494. autoEnter: item.autoEnter,
  495. content: item.content,
  496. },
  497. onSubmit: (newValues) => {
  498. item.name = newValues.name;
  499. item.position = newValues.position;
  500. item.autoEnter = newValues.autoEnter;
  501. item.content = newValues.content;
  502. // 重新渲染表格
  503. renderTable();
  504. },
  505. });
  506. }
  507. else {
  508. alert('輸入的編號不合法');
  509. }
  510. });
  511. popup.appendChild(editButton);
  512. // 創建刪除按鈕
  513. const deleteButton = document.createElement('button');
  514. deleteButton.textContent = '刪除(delete)';
  515. deleteButton.classList.add(styles['delete-button']);
  516. deleteButton.addEventListener('click', () => {
  517. // 刪除一個 item
  518. const index = prompt('請輸入要刪除的編號(delete index)');
  519. if (index && Number(index) >= 1 && index <= customize.length) {
  520. customize.splice(Number(index) - 1, 1);
  521. renderTable();
  522. }
  523. else {
  524. alert('輸入的編號不合法 (invalid index)');
  525. }
  526. });
  527. popup.appendChild(deleteButton);
  528. // 創建關閉按鈕
  529. const closeButton = document.createElement('button');
  530. closeButton.textContent = '儲存並離開(save&exit)';
  531. closeButton.classList.add(styles['close-button']);
  532. closeButton.addEventListener('click', () => {
  533. console.log(customize);
  534. // 儲存修改後的 customize 資料
  535. GM_setValue('customizeData', customize);
  536. // // 重寫一次 helper_menu
  537. // const helper_menu = document.getElementById('helper_menu');
  538. // const menu = createMenu(helper_menu);
  539. // helper_menu.replaceWith(menu);
  540. // 上面的做不出來
  541. // 所以只好重新整理頁面
  542. location.reload();
  543. document.body.removeChild(popup);
  544. });
  545. popup.appendChild(closeButton);
  546. // 創建表格
  547. const table = document.createElement('table');
  548. const tableWrapper = document.createElement('div');
  549. tableWrapper.classList.add(styles['table-wrapper']);
  550. tableWrapper.appendChild(table);
  551. popup.appendChild(tableWrapper);
  552. // 創建表頭
  553. const thead = document.createElement('thead');
  554. const tr = document.createElement('tr');
  555. const th1 = document.createElement('th');
  556. const th2 = document.createElement('th');
  557. const th3 = document.createElement('th');
  558. const th4 = document.createElement('th');
  559. const th5 = document.createElement('th');
  560. th1.textContent = '編號(index)';
  561. th2.textContent = '名稱(name)';
  562. th3.textContent = '位置(position)';
  563. th4.textContent = '自動輸入(autoEnter)?';
  564. th5.textContent = '內容(content)';
  565. tr.appendChild(th1);
  566. tr.appendChild(th2);
  567. tr.appendChild(th3);
  568. tr.appendChild(th4);
  569. tr.appendChild(th5);
  570. thead.appendChild(tr);
  571. table.appendChild(thead);
  572. // 創建表身
  573. const tbody = document.createElement('tbody');
  574. table.appendChild(tbody);
  575. // 渲染表格
  576. function renderTable() {
  577. // 先清空表格內容
  578. tbody.innerHTML = '';
  579. // 重新渲染表格
  580. customize.forEach((item, index) => {
  581. const tr = document.createElement('tr');
  582. const td1 = document.createElement('td');
  583. const td2 = document.createElement('td');
  584. const td3 = document.createElement('td');
  585. const td4 = document.createElement('td');
  586. const td5 = document.createElement('td');
  587. td1.textContent = index + 1;
  588. td2.textContent = item.name;
  589. td3.textContent = item.position;
  590. td4.textContent = item.autoEnter;
  591. td5.textContent = item.content;
  592. tr.appendChild(td1);
  593. tr.appendChild(td2);
  594. tr.appendChild(td3);
  595. tr.appendChild(td4);
  596. tr.appendChild(td5);
  597. tbody.appendChild(tr);
  598. });
  599. }
  600. // 渲染初始表格
  601. renderTable();
  602. // 點擊彈窗外的地方關閉彈窗
  603. popup.addEventListener('click', (event) => {
  604. if (event.target === popup) {
  605. document.body.removeChild(popup);
  606. }
  607. });
  608. // 將彈出視窗加入頁面中
  609. document.body.appendChild(popup);
  610. });
  611. }
  612.  
  613. main();
  614. function main() {
  615. // 頁面載入完成後執行
  616. onloadSafe(() => {
  617. // 監聽 nav 元素
  618. console.log("=====監聽 nav 元素=====");
  619. // 定義常用咒文
  620. let customize;
  621. sentinel.on("nav", (nav) => {
  622. console.log("===== trigger sentinel.on nav =====");
  623. // 讀取 customize 設定
  624. let GM_customize = GM_getValue("customizeData", customize);
  625. // 如果 user 已經有設定了就用 user 的,沒有就用預設值
  626. if (GM_customize) {
  627. customize = GM_customize;
  628. }
  629. else {
  630. customize = config.init_customize;
  631. GM_setValue("customizeData", customize);
  632. }
  633. //找不到就新增
  634. const container = document.getElementById("helper_menu");
  635. if (!container) {
  636. // 獲得目標元素
  637. const aimsNode = document.querySelector(config.NAV_MENU);
  638. // 新增一個容器
  639. const container = document.createElement("div");
  640. container.classList.add(styles$2.containerNode_class);
  641. container.id = "helper_menu";
  642. if (aimsNode) {
  643. // 設定 container 寬度為父元素寬度
  644. container.style.width = `${aimsNode.offsetWidth}px`; // 設定 container 寬度為父元素寬度
  645. // 將容器元素插入到目標元素後面
  646. aimsNode.parentNode?.insertBefore(container, aimsNode.nextSibling);
  647. // 新增一個按鈕元素
  648. addMenuBtnWrapper(container, customize, config.HELPER_MENU_TEXT);
  649. // 設定 "設定按鈕"的點擊事件
  650. setCustomizeBtn(customize);
  651. }
  652. }
  653. });
  654. });
  655. }
  656.  
  657. }));