ChatGPT-input-helper

Help organize commonly used spells quickly

目前為 2023-04-09 提交的版本,檢視 最新版本

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