Block_Obj

BLOCK_OBJ

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

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