Block_Obj

BLOCK_OBJ

目前為 2020-07-28 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/407543/831547/Block_Obj.js

  1. // ==UserScript==
  2. // @name Block_Obj
  3. // @namespace https://greasyfork.org/zh-CN/users/193133-pana
  4. // @homepage https://www.sailboatweb.com
  5. // @version 2.8.0
  6. // @description BLOCK_OBJ
  7. // @author pana
  8. // @license GNU General Public License v3.0 or later
  9. // ==/UserScript==
  10.  
  11. const BLOCK_STYLE = `
  12. .block_obj_wrap_div {
  13. background-color: #222222;
  14. border-radius: 3px;
  15. border: 1px solid #282A36;
  16. bottom: 6vh;
  17. box-shadow: 0 0 5px #282A36;
  18. color: #D3D3D3;
  19. font-size: 13px;
  20. margin: 0px;
  21. padding: 0px;
  22. position: fixed;
  23. text-align: left;
  24. transition: 0.8s;
  25. width: 520px;
  26. z-index: 99999;
  27. }
  28. .block_obj_show_wrap {
  29. display: block;
  30. right: 0;
  31. }
  32. .block_obj_hidden_wrap {
  33. right: -530px;
  34. }
  35. .block_obj_main_fieldset {
  36. border-radius: 3px;
  37. border: 3px groove #00A1D6;
  38. height: auto;
  39. margin: 8px;
  40. min-width: 300px;
  41. padding: 4px 9px 6px 9px;
  42. width: auto;
  43. }
  44. .block_obj_ul_node {
  45. list-style: none;
  46. padding-left: 0;
  47. margin: 0;
  48. }
  49. .block_obj_checkbox_li {
  50. display: inline-block;
  51. margin-top: 5px;
  52. }
  53. .block_obj_checkbox_input {
  54. clip: rect(0, 0, 0, 0);
  55. position: absolute;
  56. }
  57. .block_obj_checkbox_label {
  58. cursor: pointer;
  59. vertical-align: middle;
  60. }
  61. .block_obj_checkbox_input + label::before {
  62. background-color: silver;
  63. border-radius: 0.1em;
  64. color: #FFF;
  65. content: "\\a0";
  66. display: inline-block;
  67. height: 1em;
  68. line-height: 85%;
  69. margin-right: 0.5em;
  70. text-align: center;
  71. vertical-align: 0.2em;
  72. width: 1em;
  73. }
  74. .block_obj_checkbox_input:checked + label::before {
  75. background-color: #00A1D6;
  76. content: "\\2713";
  77. }
  78. .block_obj_separator_text {
  79. color: #FFB86C;
  80. margin-bottom: 2px;
  81. margin-top: 2px;
  82. }
  83. .block_obj_separator_symbol {
  84. background-color: #303030;
  85. height: 2px;
  86. margin-bottom: 5px;
  87. margin-top: 5px;
  88. min-width: 400px;
  89. }
  90. .block_obj_input_div {
  91. margin-top: 5px;
  92. }
  93. .block_obj_input {
  94. background-color: #C0C0C0;
  95. border: 1px solid #C0C0C0;
  96. color: #000;
  97. font-size: 13px;
  98. min-height: 15px;
  99. margin-left: 5px;
  100. margin-right: 5px;
  101. padding-left: 4px;
  102. }
  103. .block_obj_keyword_input {
  104. width: 150px;
  105. }
  106. .block_obj_input_btn {
  107. background-color: #3da9cc;
  108. border-radius: 3px;
  109. border: 1px solid #73C9E5;
  110. box-shadow: 0 0 4px #73C9E5;
  111. color: #FFF;
  112. cursor: pointer;
  113. display: inline-block;
  114. min-height: 15px;
  115. margin-left: 5px;
  116. text-align: center;
  117. vertical-align: bottom;
  118. white-space: nowrap;
  119. width: 30px;
  120. }
  121. .block_obj_list_div {
  122. margin-top: 5px;
  123. }
  124. .block_obj_list_textarea_div {
  125. border: 1px dotted #00A1D6;
  126. margin-top: 3px;
  127. max-height: 60px;
  128. min-height: 3px;
  129. overflow: auto;
  130. }
  131. .block_obj_list_textarea_div::-webkit-scrollbar {
  132. background-color: #979797;
  133. border-radius: 5px;
  134. width: 10px;
  135. }
  136. .block_obj_list_textarea_div::-webkit-scrollbar-thumb {
  137. background-color: #404040;
  138. border-radius: 5px;
  139. }
  140. .block_obj_button_clicked {
  141. color: #000;
  142. }
  143. .block_obj_child_span {
  144. background-color: #3D3D3D;
  145. border-radius: 5px;
  146. border: 1px solid #3D3D3D;
  147. display: inline-block;
  148. margin: 3px;
  149. padding: 2px;
  150. min-height: 18px;
  151. }
  152. .block_obj_child_text {
  153. border-right: 1px solid #A9181C;
  154. margin-right: 4px;
  155. padding-right: 4px;
  156. }
  157. .block_obj_child_del {
  158. color: #A9181C;
  159. cursor: pointer;
  160. }
  161. .block_obj_list_textarea_expand {
  162. max-height: 720px;
  163. }
  164. .block_obj_li_hide {
  165. display: none;
  166. }
  167. .block_obj_reg_input {
  168. width: 100px;
  169. }
  170. .block_obj_modifier_input {
  171. width: 50px;
  172. }
  173. .block_obj_button {
  174. background-color: #FB7299;
  175. border-radius: 4px;
  176. border: 1px solid #FB7299;
  177. color: #FFF;
  178. cursor: pointer;
  179. margin-top: 5px;
  180. padding: 2px 4px;
  181. position: relative;
  182. min-height: 17px;
  183. }
  184. .block_obj_save_button {
  185. float: right;
  186. margin-right: 5px;
  187. }
  188. .block_obj_cancel_button {
  189. float: left;
  190. margin-left: 5px;
  191. }
  192. .block_obj_expand_box {
  193. bottom: 0px;
  194. height: 6vh;
  195. position: fixed;
  196. right: -6vw;
  197. transition: 0.5s;
  198. width: 12vw;
  199. z-index: 99999;
  200. }
  201. .block_obj_show_expand_box {
  202. right: 0;
  203. width: 6vw;
  204. }
  205. .block_obj_expand_span {
  206. background-color: #00A1D6;
  207. border-radius: 19px;
  208. border: 1px solid #00A1D6;
  209. bottom: 1vh;
  210. color: #FFF;
  211. cursor: pointer;
  212. display: block;
  213. font-size: 13px;
  214. height: 38px;
  215. line-height: 38px;
  216. position: absolute;
  217. right: 1vw;
  218. text-align: center;
  219. width: 38px;
  220. z-index: 99999;
  221. }
  222. .block_obj_expand_span:hover {
  223. box-shadow: 0 0 5px 1px green;
  224. }
  225. .block_obj_move_right {
  226. margin-left: 15px;
  227. }
  228. .block_obj_none {
  229. display: none !important;
  230. }
  231. .block_obj_hidden {
  232. visibility: hidden !important;
  233. }
  234. .block_obj_presentation_div {
  235. display: flex;
  236. position: fixed;
  237. background-color: rgba(0, 0, 0, .5);
  238. top: 0;
  239. bottom: 0;
  240. left: 0;
  241. right: 0;
  242. z-index: 100000;
  243. align-items: center;
  244. justify-content: center;
  245. }
  246. .block_obj_dialog_div {
  247. position: relative;
  248. width: 400px;
  249. background-color: #4e5654;
  250. border: 0 solid #000;
  251. border-radius: 12px;
  252. display: flex;
  253. flex-direction: column;
  254. }
  255. .block_obj_big_bang_top_part {
  256. margin-top: 5px;
  257. margin-bottom: 5px;
  258. }
  259. .block_obj_big_bang_h3 {
  260. font-size: 20px;
  261. color: #fff;
  262. margin-left: 5px;
  263. }
  264. .block_obj_big_bang_deselect_btn {
  265. float: right;
  266. margin-top: 5px;
  267. margin-right: 5px;
  268. color: #2a2a92;
  269. cursor: pointer;
  270. }
  271. .block_obj_big_bang_middle_part {
  272. margin-top: 5px;
  273. margin-bottom: 5px;
  274. max-height: 500px;
  275. overflow-y: auto;
  276. }
  277. .block_obj_big_bang_text {
  278. background-color: #3e3939;
  279. color: #fff;
  280. padding: 5px;
  281. margin: 4px;
  282. font-size: 16px;
  283. display: inline-block;
  284. border-radius: 99px;
  285. cursor: pointer;
  286. }
  287. .block_obj_big_bang_text_selected {
  288. background-color: #3636b1 !important;
  289. }
  290. .block_obj_big_bang_bottom_part {
  291. margin-top: 5px;
  292. margin-bottom: 5px;
  293. text-align: center;
  294. }
  295. .block_obj_big_bang_add_btn, .block_obj_big_bang_copy_btn {
  296. display: inline-block;
  297. padding: 10px;
  298. color: #fff;
  299. background-color: #325561;
  300. cursor: pointer;
  301. border-radius: 99px;
  302. margin-left: 20px;
  303. margin-right: 20px;
  304. }
  305. .block_obj_big_bang_add_btn:hover, .block_obj_big_bang_copy_btn:hover {
  306. color: #2a2a92;
  307. }
  308. `;
  309. class Block_Obj {
  310. constructor(config_key, reg_key_array = []) {
  311. this.wrapDiv = null;
  312. this.mainFieldset = null;
  313. this.ulNode = null;
  314. this.style = null;
  315. this.saveField = [];
  316. this.onSave = null;
  317. this.onChange = null;
  318. this.field = [];
  319. this.configKey = config_key;
  320. this.regKeyArray = reg_key_array;
  321. this.config = this.readConfig();
  322. }
  323. init(initialization) {
  324. if (! this.id) {
  325. this.id = initialization.id ? 'blockObj_' + initialization.id : 'blockObj_' + Block_Obj.count;
  326. ! initialization.id && Block_Obj.count ++;
  327. }
  328. this.display = initialization.display ? true : false;
  329. if (initialization.events) {
  330. if (typeof(initialization.events['save']) === 'function') {
  331. this.onSave = initialization.events['save'];
  332. }
  333. if (typeof(initialization.events['change']) === 'function') {
  334. this.onChange = initialization.events['change'];
  335. }
  336. }
  337. this.style = document.createElement('style');
  338. this.style.type = 'text/css';
  339. this.style.innerHTML = BLOCK_STYLE;
  340. document.body.appendChild(this.style);
  341. if (initialization.style && typeof(initialization.style) === 'string') {
  342. let external_style = document.createElement('style');
  343. external_style.type = 'text/css';
  344. external_style.innerHTML = initialization.style;
  345. document.body.appendChild(external_style);
  346. }
  347. ! this.wrapDiv && this.createSettingsPanel();
  348. this.field = initialization.field;
  349. this.settingsPanel();
  350. initialization.menu && typeof(GM_registerMenuCommand) === 'function' && GM_registerMenuCommand(initialization.menu, () => this.expandWrap());
  351. typeof(GM_addValueChangeListener) === 'function' && GM_addValueChangeListener(this.configKey, (_name, _old_value, new_value, remote) => {
  352. if (remote) {
  353. this.destroyAndRebuild(new_value);
  354. typeof(this.onChange) === 'function' && this.onChange(this.getConfig());
  355. }
  356. });
  357. let expand_box = document.createElement('div');
  358. expand_box.className = 'block_obj_expand_box';
  359. expand_box.onmouseenter = function() {
  360. this.classList.add('block_obj_show_expand_box');
  361. };
  362. expand_box.onmouseleave = function() {
  363. this.classList.remove('block_obj_show_expand_box');
  364. };
  365. let hover_button = {};
  366. if (initialization.hover_button) {
  367. Object.assign(hover_button, initialization.hover_button);
  368. }
  369. let expand_span = document.createElement('span');
  370. expand_span.id = this.id + '_expandSpan';
  371. expand_span.className = 'block_obj_expand_span';
  372. expand_span.textContent = hover_button.label == null ? '屏蔽' : hover_button.label;
  373. expand_span.title = hover_button.title == null ? '显示/隐藏屏蔽设置' : hover_button.title;
  374. expand_span.addEventListener('click', () => {
  375. this.expandWrap();
  376. if (hover_button.click && typeof(hover_button.click) === 'function') {
  377. hover_button.click(expand_span);
  378. }
  379. });
  380. expand_box.appendChild(expand_span);
  381. document.body.appendChild(expand_box);
  382. }
  383. settingsPanel() {
  384. if (Array.isArray(this.field)) {
  385. this.field.forEach(ele => {
  386. if (! this.wrapDiv.querySelector('#' + this.id + '_' + (ele.id ? ele.id : ''))) {
  387. switch (ele.type.toLowerCase()) {
  388. case 'separator':
  389. case 's':
  390. this.insertSeparator(ele.id, ele.label, ele.title, ele.classname);
  391. break;
  392. case 'br':
  393. case 'b':
  394. this.insertBr(ele.classname);
  395. break;
  396. case 'input':
  397. case 'i':
  398. this.insertInput(ele.list_id, ele.id, false, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare);
  399. break;
  400. case 'reg_input':
  401. case 'ri':
  402. this.insertInput(ele.list_id, ele.id, true, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare, ele.modifier_label, ele.modifier_placeholder);
  403. break;
  404. case 'list':
  405. case 'l':
  406. this.insertList(ele.id, (this.config[ele.id] == null ? ele.default : this.config[ele.id]), ele.label, ele.title, ele.classname);
  407. break;
  408. case 'checkbox':
  409. case 'c':
  410. default:
  411. this.insertCheckbox(ele.id, ele.label, ele.title, (this.config[ele.id] == null ? ele.default : this.config[ele.id]), ele.classname, ele.move_right);
  412. break;
  413. }
  414. }
  415. });
  416. }
  417. let save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '保存并关闭', '保存设置并关闭设置窗口', e => {
  418. this.expandWrap();
  419. this.saveConfig();
  420. });
  421. let only_save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '仅保存', '仅保存设置', e => {
  422. only_save_button.textContent = '已保存';
  423. save_timer && window.clearTimeout(save_timer);
  424. var save_timer = window.setTimeout(() => {
  425. only_save_button.textContent = '仅保存';
  426. }, 1000);
  427. this.saveConfig();
  428. });
  429. let cancel_button = this.createSpanBtn('block_obj_button block_obj_cancel_button', '取消', '关闭设置窗口', e => {
  430. this.expandWrap();
  431. cancel_timer && window.clearTimeout(cancel_timer);
  432. var cancel_timer = window.setTimeout(() => {
  433. this.display = false;
  434. this.destroyAndRebuild();
  435. }, 800);
  436. e.stopPropagation();
  437. });
  438. this.ulNode.appendChild(save_button);
  439. this.ulNode.appendChild(only_save_button);
  440. this.ulNode.appendChild(cancel_button);
  441. document.body.appendChild(this.wrapDiv);
  442. }
  443. getConfig() {
  444. let real_config = {};
  445. Object.assign(real_config, this.config);
  446. this.regKeyArray.forEach(key => {
  447. real_config[key] = this.convertArray(real_config[key]);
  448. });
  449. return real_config;
  450. }
  451. readConfig() {
  452. let config = {};
  453. if (typeof(GM_getValue) === 'function' && this.configKey) {
  454. config = GM_getValue(this.configKey, {});
  455. }
  456. return config;
  457. }
  458. saveConfig() {
  459. this.saveField.forEach(item => {
  460. if (item.type == 'checkbox') {
  461. this.config[item.key] = document.getElementById(this.id + '_' + item.key).checked;
  462. } else if (item.type == 'list') {
  463. this.config[item.key] = this.extractList(this.id + '_' + item.key);
  464. }
  465. });
  466. typeof(GM_setValue) && GM_setValue(this.configKey, this.config);
  467. typeof(this.onSave) === 'function' && this.onSave(this.getConfig());
  468. }
  469. createSettingsPanel() {
  470. this.wrapDiv = document.createElement('div');
  471. this.wrapDiv.id = this.id + '_wrapDiv';
  472. this.wrapDiv.className = 'block_obj_wrap_div ' + (this.display ? 'block_obj_show_wrap' : 'block_obj_hidden_wrap');
  473. this.mainFieldset = document.createElement('fieldset');
  474. this.mainFieldset.id = this.id + '_mainFieldset';
  475. this.mainFieldset.className = 'block_obj_main_fieldset';
  476. this.wrapDiv.appendChild(this.mainFieldset);
  477. this.ulNode = document.createElement('ul');
  478. this.ulNode.id = this.id + '_ulNode';
  479. this.ulNode.className = 'block_obj_ul_node';
  480. this.mainFieldset.appendChild(this.ulNode);
  481. document.body.appendChild(this.wrapDiv);
  482. }
  483. destroyAndRebuild(new_config = null) {
  484. this.config = new_config || this.config;
  485. document.body.removeChild(this.wrapDiv);
  486. this.createSettingsPanel();
  487. this.settingsPanel();
  488. }
  489. expandWrap() {
  490. let panel = document.getElementById(this.id + '_wrapDiv');
  491. if (panel) {
  492. if (panel.classList.contains('block_obj_show_wrap')) {
  493. this.display = false;
  494. panel.classList.remove('block_obj_show_wrap');
  495. panel.classList.add('block_obj_hidden_wrap');
  496. } else {
  497. this.display = true;
  498. panel.classList.remove('block_obj_hidden_wrap');
  499. panel.classList.add('block_obj_show_wrap');
  500. }
  501. }
  502. }
  503. insertCheckbox(id, label = '', title = '', checked = false, classname = null, move_right = false) {
  504. let checkbox_li = document.createElement('li');
  505. checkbox_li.className = 'block_obj_checkbox_li';
  506. classname && checkbox_li.classList.add(classname);
  507. move_right && checkbox_li.classList.add('block_obj_move_right');
  508. let checkbox_input = document.createElement('input');
  509. checkbox_input.type = 'checkbox';
  510. checkbox_input.className = 'block_obj_checkbox_input';
  511. checkbox_input.id = this.id + '_' + id;
  512. checkbox_input.checked = checked ? true : false;
  513. let checkbox_label = document.createElement('label');
  514. checkbox_label.className = 'block_obj_checkbox_label';
  515. checkbox_label.setAttribute('for', this.id + '_' + id);
  516. checkbox_label.textContent = label;
  517. checkbox_label.title = title;
  518. checkbox_li.appendChild(checkbox_input);
  519. checkbox_li.appendChild(checkbox_label);
  520. this.ulNode.appendChild(checkbox_li);
  521. this.saveField.push({
  522. 'key': id,
  523. 'type': 'checkbox'
  524. });
  525. }
  526. insertSeparator(id = null, label = null, title = null, li_classname = null) {
  527. let separator_li = document.createElement('li');
  528. separator_li.className = 'block_obj_separator_li';
  529. li_classname && separator_li.classList.add('block_obj_' + li_classname);
  530. let separator_div = document.createElement('div');
  531. if (id) {
  532. separator_div.id = this.id + '_' + id;
  533. }
  534. if (label) {
  535. separator_div.className = 'block_obj_separator_text';
  536. separator_div.textContent = label;
  537. } else {
  538. separator_div.className = 'block_obj_separator_symbol';
  539. }
  540. separator_div.title = title ? title : '';
  541. separator_li.appendChild(separator_div);
  542. this.ulNode.appendChild(separator_li);
  543. }
  544. insertBr(classname = null) {
  545. let br = document.createElement('br');
  546. br.className = classname ? classname : '';
  547. this.ulNode.appendChild(br);
  548. }
  549. insertInput(list_id, id = null, is_reg = false, label = '', title = '', placeholder = '', li_classname = null, campare = null, modifier_label = '', modifier_placeholder = '') {
  550. let input_li = document.createElement('li');
  551. input_li.className = li_classname ? 'block_obj_' + li_classname : '';
  552. let input_div = document.createElement('div');
  553. input_div.className = 'block_obj_input_div';
  554. let input_span = document.createElement('span');
  555. input_span.className = 'block_obj_input_span';
  556. input_span.textContent = label;
  557. input_div.appendChild(input_span);
  558. let input = document.createElement('input');
  559. if (id) {
  560. input.id = this.id + '_' + id;
  561. }
  562. input.title = title;
  563. input.placeholder = placeholder;
  564. input.type = 'text';
  565. input.className = is_reg ? 'block_obj_input block_obj_reg_input' : 'block_obj_input block_obj_keyword_input';
  566. input_span.appendChild(input);
  567. let modifier_span = document.createElement('span');
  568. modifier_span.textContent = modifier_label;
  569. is_reg && input_div.appendChild(modifier_span);
  570. let modifier_input = document.createElement('input');
  571. if (id) {
  572. modifier_input.id = this.id + '_modifier_' + id;
  573. }
  574. modifier_input.placeholder = modifier_placeholder;
  575. modifier_input.type = 'text';
  576. modifier_input.className = 'block_obj_input block_obj_modifier_input';
  577. modifier_span.appendChild(modifier_input);
  578. let the_list_id = this.id + '_' + list_id;
  579. input.addEventListener('keyup', e => {
  580. if (e.keyCode === 13) {
  581. is_reg ? this.addRegListItem(input, modifier_input, the_list_id) : this.addListItem(input, the_list_id, campare);
  582. }
  583. });
  584. modifier_input.addEventListener('keyup', e => {
  585. e.keyCode === 13 && this.addRegListItem(input, modifier_input, the_list_id);
  586. });
  587. let add_btn = this.createSpanBtn('block_obj_input_btn', '添加', '添加内容到列表中', e => {
  588. if (is_reg ? this.addRegListItem(input, modifier_input, the_list_id) : this.addListItem(input, the_list_id, campare)) {
  589. this.buttonClicked(add_btn, '添加成功', 'block_obj_button_clicked');
  590. }
  591. });
  592. let delete_btn = this.createSpanBtn('block_obj_input_btn', '删除', '从列表中删除符合的项目', e => {
  593. if (is_reg ? this.delRegListItem(input, modifier_input, the_list_id) : this.delListItem(input, the_list_id)) {
  594. this.buttonClicked(delete_btn, '删除成功', 'block_obj_button_clicked');
  595. }
  596. });
  597. let clear_btn = this.createSpanBtn('block_obj_input_btn', '清空', '清空列表', e => {
  598. document.getElementById(the_list_id).innerHTML = '';
  599. this.buttonClicked(clear_btn, '清除成功', 'block_obj_button_clicked');
  600. });
  601. let copy_btn = this.createSpanBtn('block_obj_input_btn', '复制', '复制列表', e => {
  602. if (typeof(GM_setClipboard) === 'function') {
  603. GM_setClipboard(this.extractList(the_list_id).toString(), 'text');
  604. this.buttonClicked(copy_btn, '复制成功', 'block_obj_button_clicked');
  605. } else {
  606. alert('GM_setClipboard is not an interface.');
  607. }
  608. });
  609. let expand_btn = this.createSpanBtn('block_obj_input_btn', '展开', '展开列表', e => {
  610. li_classname && this.toggleList('block_obj_' + li_classname);
  611. if (expand_btn.textContent == '展开') {
  612. expand_btn.textContent = '恢复';
  613. expand_btn.title = '收缩列表';
  614. } else {
  615. expand_btn.textContent = '展开';
  616. expand_btn.title = '展开列表';
  617. }
  618. });
  619. input_div.appendChild(add_btn);
  620. input_div.appendChild(delete_btn);
  621. input_div.appendChild(clear_btn);
  622. input_div.appendChild(copy_btn);
  623. input_div.appendChild(expand_btn);
  624. input_li.appendChild(input_div);
  625. this.ulNode.appendChild(input_li);
  626. }
  627. insertList(id, save_array = [], label = '', title = '', li_classname = '') {
  628. let list_li = document.createElement('li');
  629. list_li.className = li_classname ? 'block_obj_' + li_classname : '';
  630. let list_div = document.createElement('div');
  631. list_div.className = 'block_obj_list_div';
  632. list_div.textContent = label;
  633. list_div.title = title;
  634. let list_textarea_div = document.createElement('div');
  635. list_textarea_div.id = this.id + '_' + id;
  636. list_textarea_div.className = 'block_obj_list_textarea_div';
  637. for (let item of save_array) {
  638. item && list_textarea_div.insertAdjacentElement('afterbegin', this.createListItem(item));
  639. }
  640. list_div.appendChild(list_textarea_div);
  641. list_li.appendChild(list_div);
  642. this.ulNode.appendChild(list_li);
  643. this.saveField.push({
  644. 'key': id,
  645. 'type': 'list'
  646. });
  647. }
  648. addListItem(input, list_id, campare = null) {
  649. let text_value = input.value;
  650. if (text_value) {
  651. let text_arr = this.stringToArray(text_value);
  652. let save_arr = this.extractList(list_id);
  653. text_arr.forEach(item => {
  654. let status = true;
  655. if (typeof(campare) === 'function') {
  656. let temp_status = campare(item);
  657. if (typeof(temp_status) === 'boolean') {
  658. status = temp_status;
  659. }
  660. }
  661. if (status) {
  662. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  663. }
  664. });
  665. input.value = '';
  666. return true;
  667. }
  668. return false;
  669. }
  670. delListItem(input, list_id) {
  671. let text_value = input.value;
  672. if (text_value) {
  673. let del_status = false;
  674. let text_arr = this.stringToArray(text_value);
  675. let save_arr = this.extractList(list_id);
  676. text_arr.forEach(item => {
  677. if (save_arr.includes(item)) {
  678. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  679. try {
  680. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(item)]);
  681. del_status = true;
  682. } catch(e) {
  683. del_status = false;
  684. console.error('Block_Obj: Error deleting element.');
  685. console.error(e);
  686. }
  687. }
  688. });
  689. if (del_status) {
  690. input.value = '';
  691. return true;
  692. }
  693. }
  694. return false;
  695. }
  696. addRegListItem(reg_input, modifier_input, list_id) {
  697. let reg_value = reg_input.value;
  698. let modifier_value = modifier_input.value;
  699. if (reg_value) {
  700. try {
  701. let reg_obj = new RegExp(reg_value, modifier_value);
  702. let save_arr = this.extractList(list_id);
  703. ! save_arr.includes(reg_obj.toString()) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(reg_obj.toString()));
  704. reg_input.value = '';
  705. modifier_input.value = '';
  706. return true;
  707. } catch(e) {
  708. console.error('Block_Obj: Invalid regular expression.');
  709. console.error(e);
  710. }
  711. }
  712. return false;
  713. }
  714. delRegListItem(reg_input, modifier_input, list_id) {
  715. let reg_value = reg_input.value;
  716. let modifier_value = modifier_input.value;
  717. if (reg_value) {
  718. let del_status = false;
  719. try {
  720. let reg_obj = new RegExp(reg_value, modifier_value);
  721. let save_arr = this.extractList(list_id);
  722. if (save_arr.includes(reg_obj.toString())) {
  723. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  724. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(reg_obj.toString())]);
  725. del_status = true;
  726. }
  727. } catch(e) {
  728. del_status = false;
  729. console.error('Block_Obj: Invalid regular expression or error deleting element.');
  730. console.error(e);
  731. }
  732. if (del_status) {
  733. reg_input.value = '';
  734. modifier_input.value = '';
  735. return true;
  736. }
  737. return false;
  738. }
  739. }
  740. createSpanBtn(classname, label, title, callback) {
  741. let btn_span = this.createBasicBtn('span', label, title, classname);
  742. btn_span.addEventListener('click', e => typeof(callback) === 'function' && callback(e));
  743. return btn_span;
  744. }
  745. createListItem(text_value) {
  746. let child_span = document.createElement('span');
  747. child_span.className = 'block_obj_child_span';
  748. let text_span = document.createElement('span');
  749. text_span.className = 'block_obj_child_text';
  750. text_span.textContent = text_value.length > 9 ? text_value.slice(0, 3) + '...' + text_value.slice(-3) : text_value;
  751. if (text_value.length > 9) {
  752. text_span.title = text_value;
  753. }
  754. let del_span = document.createElement('span');
  755. del_span.textContent = 'X';
  756. del_span.title = '移除';
  757. del_span.className = 'block_obj_child_del';
  758. del_span.addEventListener('click', e => child_span.remove());
  759. child_span.appendChild(text_span);
  760. child_span.appendChild(del_span);
  761. return child_span;
  762. }
  763. createBasicBtn(type, text, title, classname) {
  764. let btn_type = 'span';
  765. if (typeof(type) === 'string') {
  766. switch (type.toLowerCase()) {
  767. case 'div':
  768. case 'd':
  769. btn_type = 'div';
  770. break;
  771. case 'a':
  772. btn_type = 'a';
  773. break;
  774. case 'button':
  775. btn_type = 'button';
  776. break;
  777. case 'input':
  778. btn_type = 'input';
  779. break;
  780. case 'i':
  781. btn_type = 'i';
  782. break;
  783. case 'b':
  784. btn_type = 'b';
  785. break;
  786. case 'span':
  787. case 's':
  788. default:
  789. btn_type = 'span';
  790. break;
  791. }
  792. }
  793. let btn = document.createElement(btn_type);
  794. btn.textContent = text ? text : '';
  795. btn.title = title ? title : '';
  796. btn.className = classname ? classname : '';
  797. return btn;
  798. }
  799. createBlockBtn(value, list_id, classname, type = 'span', text = '', title = '', campare = null) {
  800. let block_btn = this.createBasicBtn(type, text, title, classname);
  801. block_btn.addEventListener('click', e => {
  802. e.stopPropagation();
  803. if (value && list_id) {
  804. let status = true;
  805. if (typeof(campare) === 'function') {
  806. let temp_status = campare(value);
  807. if (typeof(temp_status) === 'boolean') {
  808. status = temp_status;
  809. }
  810. }
  811. if (status) {
  812. let the_list_id = this.id + '_' + list_id;
  813. let save_arr = this.extractList(the_list_id);
  814. ! save_arr.includes(value) && document.getElementById(the_list_id) && document.getElementById(the_list_id).insertAdjacentElement('afterbegin', this.createListItem(value));
  815. }
  816. }
  817. this.saveConfig();
  818. });
  819. return block_btn;
  820. }
  821. createBigBangBtn(value, list_id, classname, type = 'span', text = '', title = '', campare = null) {
  822. let big_bang_btn = this.createBasicBtn(type, text, title, classname);
  823. big_bang_btn.addEventListener('click', e => {
  824. e.stopPropagation();
  825. if (value && list_id) {
  826. let the_list_id = this.id + '_' + list_id;
  827. this.injectionBigBangPanel(value, the_list_id, campare);
  828. }
  829. });
  830. return big_bang_btn;
  831. }
  832. injectionBigBangPanel(value, list_id, campare = null) {
  833. let presentation_div = document.createElement('div');
  834. presentation_div.id = this.id + '_presentationDiv';
  835. presentation_div.className = 'block_obj_presentation_div';
  836. presentation_div.addEventListener('click', function(event) {
  837. if (event.target === this) {
  838. if (presentation_div) {
  839. presentation_div.remove();
  840. }
  841. }
  842. });
  843. let dialog_div = document.createElement('div');
  844. dialog_div.className = 'block_obj_dialog_div';
  845. presentation_div.appendChild(dialog_div);
  846. let top_part = document.createElement('div');
  847. top_part.className = 'block_obj_big_bang_top_part';
  848. let h3 = document.createElement('h3');
  849. h3.className = 'block_obj_big_bang_h3';
  850. h3.textContent = '大爆炸';
  851. top_part.appendChild(h3);
  852. let deselect_btn = this.createSpanBtn('block_obj_big_bang_deselect_btn', '取消选择', '取消全部已选择的内容', e => {
  853. e.stopPropagation();
  854. let select_arr = document.querySelectorAll('.block_obj_big_bang_text_selected');
  855. for (let s of select_arr) {
  856. s.classList.remove('block_obj_big_bang_text_selected');
  857. }
  858. });
  859. top_part.appendChild(deselect_btn);
  860. let value_array = value.replace(/\s| /gi, '').split('');
  861. let middle_part = document.createElement('div');
  862. middle_part.className = 'block_obj_big_bang_middle_part';
  863. let node_array = value_array.map((ele, index) => {
  864. let ele_node = this.createBasicBtn('span', ele, '', 'block_obj_big_bang_text');
  865. ele_node.setAttribute('data-index', index);
  866. ele_node.addEventListener('click', e => {
  867. e.stopPropagation();
  868. ele_node.classList.contains('block_obj_big_bang_text_selected') ? ele_node.classList.remove('block_obj_big_bang_text_selected') : ele_node.classList.add('block_obj_big_bang_text_selected');
  869. });
  870. return ele_node;
  871. });
  872. node_array.forEach(item => {
  873. middle_part.appendChild(item);
  874. });
  875. let bottom_part = document.createElement('div');
  876. bottom_part.className = 'block_obj_big_bang_bottom_part';
  877. let add_btn = this.createSpanBtn('block_obj_big_bang_add_btn', '添加', '添加选择的内容到列表中', e => {
  878. e.stopPropagation();
  879. let text_value = this.getSelectedText('block_obj_big_bang_text_selected');
  880. if (text_value && list_id) {
  881. let text_arr = this.stringToArray(text_value);
  882. let save_arr = this.extractList(list_id);
  883. text_arr.forEach(item => {
  884. let status = true;
  885. if (typeof(campare) === 'function') {
  886. let temp_status = campare(item);
  887. if (typeof(temp_status) === 'boolean') {
  888. status = temp_status;
  889. }
  890. }
  891. if (status) {
  892. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  893. }
  894. });
  895. this.saveConfig();
  896. presentation_div.remove();
  897. }
  898. });
  899. bottom_part.appendChild(add_btn);
  900. let copy_btn = this.createSpanBtn('block_obj_big_bang_copy_btn', '复制', '复制选择的内容到剪贴板中', e => {
  901. e.stopPropagation();
  902. let text_value = this.getSelectedText('block_obj_big_bang_text_selected');
  903. if (text_value) {
  904. GM_setClipboard(text_value, 'text');
  905. presentation_div.remove();
  906. }
  907. });
  908. bottom_part.appendChild(copy_btn);
  909. dialog_div.appendChild(top_part);
  910. dialog_div.appendChild(middle_part);
  911. dialog_div.appendChild(bottom_part);
  912. document.body.appendChild(presentation_div);
  913. }
  914. getSelectedText(classname) {
  915. let selected_array = document.getElementsByClassName(classname);
  916. let last_index = -1;
  917. let text_value = '';
  918. for (let selected of selected_array) {
  919. let index = selected.getAttribute('data-index') || -1;
  920. if (Number(index) == last_index + 1) {
  921. text_value += selected.textContent;
  922. } else {
  923. text_value = text_value ? text_value + ',' + selected.textContent : selected.textContent;
  924. }
  925. last_index = Number(index);
  926. }
  927. return text_value;
  928. }
  929. buttonClicked(button, click_title, click_class) {
  930. let original_title = button.title;
  931. button.title = click_title;
  932. click_class && button.classList.add(click_class);
  933. timer && window.clearTimeout(timer);
  934. var timer = window.setTimeout(() => {
  935. button.title = original_title;
  936. button.classList.remove(click_class);
  937. }, 1000);
  938. }
  939. extractList(list_id) {
  940. let re_arr = [];
  941. let list_dom = document.getElementById(list_id);
  942. let list_arr = list_dom.getElementsByClassName('block_obj_child_text');
  943. for (let i = list_arr.length - 1; i >= 0; i --) {
  944. list_arr[i].title ? re_arr.push(list_arr[i].title) : re_arr.push(list_arr[i].textContent);
  945. }
  946. return re_arr;
  947. }
  948. stringToArray(text_string) {
  949. let temp_array = text_string.split(',');
  950. let return_array = [];
  951. for (let i = 0, l = temp_array.length; i < l; i ++) {
  952. for (let j = i + 1; j < l; j ++) {
  953. if (temp_array[i] === temp_array[j]) {
  954. ++ i;
  955. j = i;
  956. }
  957. }
  958. return_array.push(temp_array[i]);
  959. }
  960. return return_array;
  961. }
  962. toggleList(li_classname) {
  963. for (let li of this.ulNode.querySelectorAll('li')) {
  964. if (li.classList.contains(li_classname)) {
  965. let list_textarea_div = li.querySelector('.block_obj_list_textarea_div');
  966. if (list_textarea_div) {
  967. list_textarea_div.classList.contains('block_obj_list_textarea_expand') ? list_textarea_div.classList.remove('block_obj_list_textarea_expand') : list_textarea_div.classList.add('block_obj_list_textarea_expand');
  968. }
  969. } else {
  970. li.classList.contains('block_obj_li_hide') ? li.classList.remove('block_obj_li_hide') : li.classList.add('block_obj_li_hide');
  971. }
  972. }
  973. for (let br of this.ulNode.querySelectorAll('br')) {
  974. br.classList.contains('block_obj_li_hide') ? br.classList.remove('block_obj_li_hide') : br.classList.add('block_obj_li_hide');
  975. }
  976. }
  977. convertArray(string_array) {
  978. let re_arr = [];
  979. if (Array.isArray(string_array)) {
  980. for (let i in string_array) {
  981. try {
  982. let new_reg = new RegExp(string_array[i].replace(/^\/|\/[a-z]*$/gi, ''), string_array[i].replace(/^\/.*\/[^a-z]*/i, ''));
  983. re_arr.push(new_reg);
  984. } catch(e) {
  985. console.error('Block_Obj: The transformation contains invalid regular expressions.');
  986. console.error(e);
  987. }
  988. }
  989. }
  990. return re_arr;
  991. }
  992. }
  993. Block_Obj.count = 0;
  994. Block_Obj.fn = {
  995. 'hideOperation': (node, hide_status, method = 0) => {
  996. if (node && node instanceof HTMLElement) {
  997. if (hide_status) {
  998. if (method === 0) {
  999. node.classList.add('block_obj_none');
  1000. } else if (method === 1) {
  1001. node.classList.add('block_obj_hidden');
  1002. } else if (method === 2) {
  1003. node.style.display = 'none';
  1004. } else if (method === 3) {
  1005. node.style.visibility = 'hidden';
  1006. }
  1007. } else {
  1008. if (method === 0) {
  1009. node.classList.remove('block_obj_none');
  1010. } else if (method === 1) {
  1011. node.classList.remove('block_obj_hidden');
  1012. } else if (method === 2) {
  1013. node.style.display = '';
  1014. } else if (method === 3) {
  1015. node.style.visibility = '';
  1016. }
  1017. }
  1018. }
  1019. }
  1020. };