Block_Obj

BLOCK_OBJ

当前为 2020-07-28 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/407543/831532/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.5
  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 expand_span = document.createElement('span');
  366. expand_span.id = this.id + '_expandSpan';
  367. expand_span.className = 'block_obj_expand_span';
  368. expand_span.textContent = '屏蔽';
  369. expand_span.title = '显示屏蔽设置';
  370. expand_span.addEventListener('click', () => this.expandWrap());
  371. expand_box.appendChild(expand_span);
  372. document.body.appendChild(expand_box);
  373. }
  374. settingsPanel() {
  375. if (Array.isArray(this.field)) {
  376. this.field.forEach(ele => {
  377. if (! this.wrapDiv.querySelector('#' + this.id + '_' + (ele.id ? ele.id : ''))) {
  378. switch (ele.type.toLowerCase()) {
  379. case 'separator':
  380. case 's':
  381. this.insertSeparator(ele.id, ele.label, ele.title, ele.classname);
  382. break;
  383. case 'br':
  384. case 'b':
  385. this.insertBr(ele.classname);
  386. break;
  387. case 'input':
  388. case 'i':
  389. this.insertInput(ele.list_id, ele.id, false, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare);
  390. break;
  391. case 'reg_input':
  392. case 'ri':
  393. this.insertInput(ele.list_id, ele.id, true, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare, ele.modifier_label, ele.modifier_placeholder);
  394. break;
  395. case 'list':
  396. case 'l':
  397. this.insertList(ele.id, (this.config[ele.id] == null ? ele.default : this.config[ele.id]), ele.label, ele.title, ele.classname);
  398. break;
  399. case 'checkbox':
  400. case 'c':
  401. default:
  402. this.insertCheckbox(ele.id, ele.label, ele.title, (this.config[ele.id] == null ? ele.default : this.config[ele.id]), ele.classname, ele.move_right);
  403. break;
  404. }
  405. }
  406. });
  407. }
  408. let save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '保存并关闭', '保存设置并关闭设置窗口', e => {
  409. this.expandWrap();
  410. this.saveConfig();
  411. });
  412. let only_save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '仅保存', '仅保存设置', e => {
  413. only_save_button.textContent = '已保存';
  414. save_timer && window.clearTimeout(save_timer);
  415. var save_timer = window.setTimeout(() => {
  416. only_save_button.textContent = '仅保存';
  417. }, 1000);
  418. this.saveConfig();
  419. });
  420. let cancel_button = this.createSpanBtn('block_obj_button block_obj_cancel_button', '取消', '关闭设置窗口', e => {
  421. this.expandWrap();
  422. cancel_timer && window.clearTimeout(cancel_timer);
  423. var cancel_timer = window.setTimeout(() => {
  424. this.display = false;
  425. this.destroyAndRebuild();
  426. }, 800);
  427. e.stopPropagation();
  428. });
  429. this.ulNode.appendChild(save_button);
  430. this.ulNode.appendChild(only_save_button);
  431. this.ulNode.appendChild(cancel_button);
  432. document.body.appendChild(this.wrapDiv);
  433. }
  434. getConfig() {
  435. let real_config = {};
  436. Object.assign(real_config, this.config);
  437. this.regKeyArray.forEach(key => {
  438. real_config[key] = this.convertArray(real_config[key]);
  439. });
  440. return real_config;
  441. }
  442. readConfig() {
  443. let config = {};
  444. if (typeof(GM_getValue) === 'function' && this.configKey) {
  445. config = GM_getValue(this.configKey, {});
  446. }
  447. return config;
  448. }
  449. saveConfig() {
  450. this.saveField.forEach(item => {
  451. if (item.type == 'checkbox') {
  452. this.config[item.key] = document.getElementById(this.id + '_' + item.key).checked;
  453. } else if (item.type == 'list') {
  454. this.config[item.key] = this.extractList(this.id + '_' + item.key);
  455. }
  456. });
  457. typeof(GM_setValue) && GM_setValue(this.configKey, this.config);
  458. typeof(this.onSave) === 'function' && this.onSave(this.getConfig());
  459. }
  460. createSettingsPanel() {
  461. this.wrapDiv = document.createElement('div');
  462. this.wrapDiv.id = this.id + '_wrapDiv';
  463. this.wrapDiv.className = 'block_obj_wrap_div ' + (this.display ? 'block_obj_show_wrap' : 'block_obj_hidden_wrap');
  464. this.mainFieldset = document.createElement('fieldset');
  465. this.mainFieldset.id = this.id + '_mainFieldset';
  466. this.mainFieldset.className = 'block_obj_main_fieldset';
  467. this.wrapDiv.appendChild(this.mainFieldset);
  468. this.ulNode = document.createElement('ul');
  469. this.ulNode.id = this.id + '_ulNode';
  470. this.ulNode.className = 'block_obj_ul_node';
  471. this.mainFieldset.appendChild(this.ulNode);
  472. document.body.appendChild(this.wrapDiv);
  473. }
  474. destroyAndRebuild(new_config = null) {
  475. this.config = new_config || this.config;
  476. document.body.removeChild(this.wrapDiv);
  477. this.createSettingsPanel();
  478. this.settingsPanel();
  479. }
  480. expandWrap() {
  481. let panel = document.getElementById(this.id + '_wrapDiv');
  482. let button = document.getElementById(this.id + '_expandSpan');
  483. if (panel) {
  484. if (panel.classList.contains('block_obj_show_wrap')) {
  485. this.display = false;
  486. panel.classList.remove('block_obj_show_wrap');
  487. panel.classList.add('block_obj_hidden_wrap');
  488. if (button) {
  489. button.title = '显示屏蔽设置';
  490. }
  491. } else {
  492. this.display = true;
  493. panel.classList.remove('block_obj_hidden_wrap');
  494. panel.classList.add('block_obj_show_wrap');
  495. if (button) {
  496. button.title = '隐藏屏蔽设置';
  497. }
  498. }
  499. }
  500. }
  501. insertCheckbox(id, label = '', title = '', checked = false, classname = null, move_right = false) {
  502. let checkbox_li = document.createElement('li');
  503. checkbox_li.className = 'block_obj_checkbox_li';
  504. classname && checkbox_li.classList.add(classname);
  505. move_right && checkbox_li.classList.add('block_obj_move_right');
  506. let checkbox_input = document.createElement('input');
  507. checkbox_input.type = 'checkbox';
  508. checkbox_input.className = 'block_obj_checkbox_input';
  509. checkbox_input.id = this.id + '_' + id;
  510. checkbox_input.checked = checked ? true : false;
  511. let checkbox_label = document.createElement('label');
  512. checkbox_label.className = 'block_obj_checkbox_label';
  513. checkbox_label.setAttribute('for', this.id + '_' + id);
  514. checkbox_label.textContent = label;
  515. checkbox_label.title = title;
  516. checkbox_li.appendChild(checkbox_input);
  517. checkbox_li.appendChild(checkbox_label);
  518. this.ulNode.appendChild(checkbox_li);
  519. this.saveField.push({
  520. 'key': id,
  521. 'type': 'checkbox'
  522. });
  523. }
  524. insertSeparator(id = null, label = null, title = null, li_classname = null) {
  525. let separator_li = document.createElement('li');
  526. separator_li.className = 'block_obj_separator_li';
  527. li_classname && separator_li.classList.add('block_obj_' + li_classname);
  528. let separator_div = document.createElement('div');
  529. if (id) {
  530. separator_div.id = this.id + '_' + id;
  531. }
  532. if (label) {
  533. separator_div.className = 'block_obj_separator_text';
  534. separator_div.textContent = label;
  535. } else {
  536. separator_div.className = 'block_obj_separator_symbol';
  537. }
  538. separator_div.title = title ? title : '';
  539. separator_li.appendChild(separator_div);
  540. this.ulNode.appendChild(separator_li);
  541. }
  542. insertBr(classname = null) {
  543. let br = document.createElement('br');
  544. br.className = classname ? classname : '';
  545. this.ulNode.appendChild(br);
  546. }
  547. insertInput(list_id, id = null, is_reg = false, label = '', title = '', placeholder = '', li_classname = null, campare = null, modifier_label = '', modifier_placeholder = '') {
  548. let input_li = document.createElement('li');
  549. input_li.className = li_classname ? 'block_obj_' + li_classname : '';
  550. let input_div = document.createElement('div');
  551. input_div.className = 'block_obj_input_div';
  552. let input_span = document.createElement('span');
  553. input_span.className = 'block_obj_input_span';
  554. input_span.textContent = label;
  555. input_div.appendChild(input_span);
  556. let input = document.createElement('input');
  557. if (id) {
  558. input.id = this.id + '_' + id;
  559. }
  560. input.title = title;
  561. input.placeholder = placeholder;
  562. input.type = 'text';
  563. input.className = is_reg ? 'block_obj_input block_obj_reg_input' : 'block_obj_input block_obj_keyword_input';
  564. input_span.appendChild(input);
  565. let modifier_span = document.createElement('span');
  566. modifier_span.textContent = modifier_label;
  567. is_reg && input_div.appendChild(modifier_span);
  568. let modifier_input = document.createElement('input');
  569. if (id) {
  570. modifier_input.id = this.id + '_modifier_' + id;
  571. }
  572. modifier_input.placeholder = modifier_placeholder;
  573. modifier_input.type = 'text';
  574. modifier_input.className = 'block_obj_input block_obj_modifier_input';
  575. modifier_span.appendChild(modifier_input);
  576. let the_list_id = this.id + '_' + list_id;
  577. input.addEventListener('keyup', e => {
  578. if (e.keyCode === 13) {
  579. is_reg ? this.addRegListItem(input, modifier_input, the_list_id) : this.addListItem(input, the_list_id, campare);
  580. }
  581. });
  582. modifier_input.addEventListener('keyup', e => {
  583. e.keyCode === 13 && this.addRegListItem(input, modifier_input, the_list_id);
  584. });
  585. let add_btn = this.createSpanBtn('block_obj_input_btn', '添加', '添加内容到列表中', e => {
  586. if (is_reg ? this.addRegListItem(input, modifier_input, the_list_id) : this.addListItem(input, the_list_id, campare)) {
  587. this.buttonClicked(add_btn, '添加成功', 'block_obj_button_clicked');
  588. }
  589. });
  590. let delete_btn = this.createSpanBtn('block_obj_input_btn', '删除', '从列表中删除符合的项目', e => {
  591. if (is_reg ? this.delRegListItem(input, modifier_input, the_list_id) : this.delListItem(input, the_list_id)) {
  592. this.buttonClicked(delete_btn, '删除成功', 'block_obj_button_clicked');
  593. }
  594. });
  595. let clear_btn = this.createSpanBtn('block_obj_input_btn', '清空', '清空列表', e => {
  596. document.getElementById(the_list_id).innerHTML = '';
  597. this.buttonClicked(clear_btn, '清除成功', 'block_obj_button_clicked');
  598. });
  599. let copy_btn = this.createSpanBtn('block_obj_input_btn', '复制', '复制列表', e => {
  600. if (typeof(GM_setClipboard) == 'function') {
  601. GM_setClipboard(this.extractList(the_list_id).toString(), 'text');
  602. this.buttonClicked(copy_btn, '复制成功', 'block_obj_button_clicked');
  603. } else {
  604. alert('GM_setClipboard is not an interface.');
  605. }
  606. });
  607. let expand_btn = this.createSpanBtn('block_obj_input_btn', '展开', '展开列表', e => {
  608. li_classname && this.toggleList('block_obj_' + li_classname);
  609. if (expand_btn.textContent == '展开') {
  610. expand_btn.textContent = '恢复';
  611. expand_btn.title = '收缩列表';
  612. } else {
  613. expand_btn.textContent = '展开';
  614. expand_btn.title = '展开列表';
  615. }
  616. });
  617. input_div.appendChild(add_btn);
  618. input_div.appendChild(delete_btn);
  619. input_div.appendChild(clear_btn);
  620. input_div.appendChild(copy_btn);
  621. input_div.appendChild(expand_btn);
  622. input_li.appendChild(input_div);
  623. this.ulNode.appendChild(input_li);
  624. }
  625. insertList(id, save_array = [], label = '', title = '', li_classname = '') {
  626. let list_li = document.createElement('li');
  627. list_li.className = li_classname ? 'block_obj_' + li_classname : '';
  628. let list_div = document.createElement('div');
  629. list_div.className = 'block_obj_list_div';
  630. list_div.textContent = label;
  631. list_div.title = title;
  632. let list_textarea_div = document.createElement('div');
  633. list_textarea_div.id = this.id + '_' + id;
  634. list_textarea_div.className = 'block_obj_list_textarea_div';
  635. for (let item of save_array) {
  636. item && list_textarea_div.insertAdjacentElement('afterbegin', this.createListItem(item));
  637. }
  638. list_div.appendChild(list_textarea_div);
  639. list_li.appendChild(list_div);
  640. this.ulNode.appendChild(list_li);
  641. this.saveField.push({
  642. 'key': id,
  643. 'type': 'list'
  644. });
  645. }
  646. addListItem(input, list_id, campare = null) {
  647. let text_value = input.value;
  648. if (text_value) {
  649. let text_arr = this.stringToArray(text_value);
  650. let save_arr = this.extractList(list_id);
  651. text_arr.forEach(item => {
  652. let status = true;
  653. if (typeof(campare) === 'function') {
  654. let temp_status = campare(item);
  655. if (typeof(temp_status) === 'boolean') {
  656. status = temp_status;
  657. }
  658. }
  659. if (status) {
  660. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  661. }
  662. });
  663. input.value = '';
  664. return true;
  665. }
  666. return false;
  667. }
  668. delListItem(input, list_id) {
  669. let text_value = input.value;
  670. if (text_value) {
  671. let del_status = false;
  672. let text_arr = this.stringToArray(text_value);
  673. let save_arr = this.extractList(list_id);
  674. text_arr.forEach(item => {
  675. if (save_arr.includes(item)) {
  676. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  677. try {
  678. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(item)]);
  679. del_status = true;
  680. } catch(e) {
  681. del_status = false;
  682. console.error('Block_Obj: Error deleting element.');
  683. console.error(e);
  684. }
  685. }
  686. });
  687. if (del_status) {
  688. input.value = '';
  689. return true;
  690. }
  691. }
  692. return false;
  693. }
  694. addRegListItem(reg_input, modifier_input, list_id) {
  695. let reg_value = reg_input.value;
  696. let modifier_value = modifier_input.value;
  697. if (reg_value) {
  698. try {
  699. let reg_obj = new RegExp(reg_value, modifier_value);
  700. let save_arr = this.extractList(list_id);
  701. ! save_arr.includes(reg_obj.toString()) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(reg_obj.toString()));
  702. reg_input.value = '';
  703. modifier_input.value = '';
  704. return true;
  705. } catch(e) {
  706. console.error('Block_Obj: Invalid regular expression.');
  707. console.error(e);
  708. }
  709. }
  710. return false;
  711. }
  712. delRegListItem(reg_input, modifier_input, list_id) {
  713. let reg_value = reg_input.value;
  714. let modifier_value = modifier_input.value;
  715. if (reg_value) {
  716. let del_status = false;
  717. try {
  718. let reg_obj = new RegExp(reg_value, modifier_value);
  719. let save_arr = this.extractList(list_id);
  720. if (save_arr.includes(reg_obj.toString())) {
  721. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  722. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(reg_obj.toString())]);
  723. del_status = true;
  724. }
  725. } catch(e) {
  726. del_status = false;
  727. console.error('Block_Obj: Invalid regular expression or error deleting element.');
  728. console.error(e);
  729. }
  730. if (del_status) {
  731. reg_input.value = '';
  732. modifier_input.value = '';
  733. return true;
  734. }
  735. return false;
  736. }
  737. }
  738. createSpanBtn(classname, label, title, callback) {
  739. let btn_span = this.createBasicBtn('span', label, title, classname);
  740. btn_span.addEventListener('click', e => typeof(callback) === 'function' && callback(e));
  741. return btn_span;
  742. }
  743. createListItem(text_value) {
  744. let child_span = document.createElement('span');
  745. child_span.className = 'block_obj_child_span';
  746. let text_span = document.createElement('span');
  747. text_span.className = 'block_obj_child_text';
  748. text_span.textContent = text_value.length > 9 ? text_value.slice(0, 3) + '...' + text_value.slice(-3) : text_value;
  749. if (text_value.length > 9) {
  750. text_span.title = text_value;
  751. }
  752. let del_span = document.createElement('span');
  753. del_span.textContent = 'X';
  754. del_span.title = '移除';
  755. del_span.className = 'block_obj_child_del';
  756. del_span.addEventListener('click', e => child_span.remove());
  757. child_span.appendChild(text_span);
  758. child_span.appendChild(del_span);
  759. return child_span;
  760. }
  761. createBasicBtn(type, text, title, classname) {
  762. let btn_type = 'span';
  763. if (typeof(type) === 'string') {
  764. switch (type.toLowerCase()) {
  765. case 'div':
  766. case 'd':
  767. btn_type = 'div';
  768. break;
  769. case 'a':
  770. btn_type = 'a';
  771. break;
  772. case 'button':
  773. btn_type = 'button';
  774. break;
  775. case 'input':
  776. btn_type = 'input';
  777. break;
  778. case 'i':
  779. btn_type = 'i';
  780. break;
  781. case 'b':
  782. btn_type = 'b';
  783. break;
  784. case 'span':
  785. case 's':
  786. default:
  787. btn_type = 'span';
  788. break;
  789. }
  790. }
  791. let btn = document.createElement(btn_type);
  792. btn.textContent = text ? text : '';
  793. btn.title = title ? title : '';
  794. btn.className = classname ? classname : '';
  795. return btn;
  796. }
  797. createBlockBtn(value, list_id, classname, type = 'span', text = '', title = '', campare = null) {
  798. let block_btn = this.createBasicBtn(type, text, title, classname);
  799. block_btn.addEventListener('click', e => {
  800. e.stopPropagation();
  801. if (value && list_id) {
  802. let status = true;
  803. if (typeof(campare) === 'function') {
  804. let temp_status = campare(value);
  805. if (typeof(temp_status) === 'boolean') {
  806. status = temp_status;
  807. }
  808. }
  809. if (status) {
  810. let the_list_id = this.id + '_' + list_id;
  811. let save_arr = this.extractList(the_list_id);
  812. ! save_arr.includes(value) && document.getElementById(the_list_id) && document.getElementById(the_list_id).insertAdjacentElement('afterbegin', this.createListItem(value));
  813. }
  814. }
  815. this.saveConfig();
  816. });
  817. return block_btn;
  818. }
  819. createBigBangBtn(value, list_id, classname, type = 'span', text = '', title = '', campare = null) {
  820. let big_bang_btn = this.createBasicBtn(type, text, title, classname);
  821. big_bang_btn.addEventListener('click', e => {
  822. e.stopPropagation();
  823. if (value && list_id) {
  824. let the_list_id = this.id + '_' + list_id;
  825. this.injectionBigBangPanel(value, the_list_id, campare);
  826. }
  827. });
  828. return big_bang_btn;
  829. }
  830. injectionBigBangPanel(value, list_id, campare = null) {
  831. let presentation_div = document.createElement('div');
  832. presentation_div.id = this.id + '_presentationDiv';
  833. presentation_div.className = 'block_obj_presentation_div';
  834. presentation_div.addEventListener('click', function(event) {
  835. if (event.target === this) {
  836. if (presentation_div) {
  837. presentation_div.remove();
  838. }
  839. }
  840. });
  841. let dialog_div = document.createElement('div');
  842. dialog_div.className = 'block_obj_dialog_div';
  843. presentation_div.appendChild(dialog_div);
  844. let top_part = document.createElement('div');
  845. top_part.className = 'block_obj_big_bang_top_part';
  846. let h3 = document.createElement('h3');
  847. h3.className = 'block_obj_big_bang_h3';
  848. h3.textContent = '大爆炸';
  849. top_part.appendChild(h3);
  850. let deselect_btn = this.createSpanBtn('block_obj_big_bang_deselect_btn', '取消选择', '取消全部已选择的内容', e => {
  851. e.stopPropagation();
  852. let select_arr = document.querySelectorAll('.block_obj_big_bang_text_selected');
  853. for (let s of select_arr) {
  854. s.classList.remove('block_obj_big_bang_text_selected');
  855. }
  856. });
  857. top_part.appendChild(deselect_btn);
  858. let value_array = value.replace(/\s| /gi, '').split('');
  859. let middle_part = document.createElement('div');
  860. middle_part.className = 'block_obj_big_bang_middle_part';
  861. let node_array = value_array.map((ele, index) => {
  862. let ele_node = this.createBasicBtn('span', ele, '', 'block_obj_big_bang_text');
  863. ele_node.setAttribute('data-index', index);
  864. ele_node.addEventListener('click', e => {
  865. e.stopPropagation();
  866. 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');
  867. });
  868. return ele_node;
  869. });
  870. node_array.forEach(item => {
  871. middle_part.appendChild(item);
  872. });
  873. let bottom_part = document.createElement('div');
  874. bottom_part.className = 'block_obj_big_bang_bottom_part';
  875. let add_btn = this.createSpanBtn('block_obj_big_bang_add_btn', '添加', '添加选择的内容到列表中', e => {
  876. e.stopPropagation();
  877. let text_value = this.getSelectedText('block_obj_big_bang_text_selected');
  878. if (text_value && list_id) {
  879. let text_arr = this.stringToArray(text_value);
  880. let save_arr = this.extractList(list_id);
  881. text_arr.forEach(item => {
  882. let status = true;
  883. if (typeof(campare) === 'function') {
  884. let temp_status = campare(item);
  885. if (typeof(temp_status) === 'boolean') {
  886. status = temp_status;
  887. }
  888. }
  889. if (status) {
  890. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  891. }
  892. });
  893. this.saveConfig();
  894. presentation_div.remove();
  895. }
  896. });
  897. bottom_part.appendChild(add_btn);
  898. let copy_btn = this.createSpanBtn('block_obj_big_bang_copy_btn', '复制', '复制选择的内容到剪贴板中', e => {
  899. e.stopPropagation();
  900. let text_value = this.getSelectedText('block_obj_big_bang_text_selected');
  901. if (text_value) {
  902. GM_setClipboard(text_value, 'text');
  903. presentation_div.remove();
  904. }
  905. });
  906. bottom_part.appendChild(copy_btn);
  907. dialog_div.appendChild(top_part);
  908. dialog_div.appendChild(middle_part);
  909. dialog_div.appendChild(bottom_part);
  910. document.body.appendChild(presentation_div);
  911. }
  912. getSelectedText(classname) {
  913. let selected_array = document.getElementsByClassName(classname);
  914. let last_index = -1;
  915. let text_value = '';
  916. for (let selected of selected_array) {
  917. let index = selected.getAttribute('data-index') || -1;
  918. if (Number(index) == last_index + 1) {
  919. text_value += selected.textContent;
  920. } else {
  921. text_value = text_value ? text_value + ',' + selected.textContent : selected.textContent;
  922. }
  923. last_index = Number(index);
  924. }
  925. return text_value;
  926. }
  927. buttonClicked(button, click_title, click_class) {
  928. let original_title = button.title;
  929. button.title = click_title;
  930. click_class && button.classList.add(click_class);
  931. timer && window.clearTimeout(timer);
  932. var timer = window.setTimeout(() => {
  933. button.title = original_title;
  934. button.classList.remove(click_class);
  935. }, 1000);
  936. }
  937. extractList(list_id) {
  938. let re_arr = [];
  939. let list_dom = document.getElementById(list_id);
  940. let list_arr = list_dom.getElementsByClassName('block_obj_child_text');
  941. for (let i = list_arr.length - 1; i >= 0; i --) {
  942. list_arr[i].title ? re_arr.push(list_arr[i].title) : re_arr.push(list_arr[i].textContent);
  943. }
  944. return re_arr;
  945. }
  946. stringToArray(text_string) {
  947. let temp_array = text_string.split(',');
  948. let return_array = [];
  949. for (let i = 0, l = temp_array.length; i < l; i ++) {
  950. for (let j = i + 1; j < l; j ++) {
  951. if (temp_array[i] === temp_array[j]) {
  952. ++ i;
  953. j = i;
  954. }
  955. }
  956. return_array.push(temp_array[i]);
  957. }
  958. return return_array;
  959. }
  960. toggleList(li_classname) {
  961. for (let li of this.ulNode.querySelectorAll('li')) {
  962. if (li.classList.contains(li_classname)) {
  963. let list_textarea_div = li.querySelector('.block_obj_list_textarea_div');
  964. if (list_textarea_div) {
  965. 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');
  966. }
  967. } else {
  968. li.classList.contains('block_obj_li_hide') ? li.classList.remove('block_obj_li_hide') : li.classList.add('block_obj_li_hide');
  969. }
  970. }
  971. for (let br of this.ulNode.querySelectorAll('br')) {
  972. br.classList.contains('block_obj_li_hide') ? br.classList.remove('block_obj_li_hide') : br.classList.add('block_obj_li_hide');
  973. }
  974. }
  975. convertArray(string_array) {
  976. let re_arr = [];
  977. if (Array.isArray(string_array)) {
  978. for (let i in string_array) {
  979. try {
  980. let new_reg = new RegExp(string_array[i].replace(/^\/|\/[a-z]*$/gi, ''), string_array[i].replace(/^\/.*\/[^a-z]*/i, ''));
  981. re_arr.push(new_reg);
  982. } catch(e) {
  983. console.error('Block_Obj: The transformation contains invalid regular expressions.');
  984. console.error(e);
  985. }
  986. }
  987. }
  988. return re_arr;
  989. }
  990. }
  991. Block_Obj.count = 0;
  992. Block_Obj.fn = {
  993. 'hideOperation': (node, hide_status, method = 0) => {
  994. if (node && node instanceof HTMLElement) {
  995. if (hide_status) {
  996. if (method === 0) {
  997. node.classList.add('block_obj_none');
  998. } else if (method === 1) {
  999. node.classList.add('block_obj_hidden');
  1000. } else if (method === 2) {
  1001. node.style.display = 'none';
  1002. } else if (method === 3) {
  1003. node.style.visibility = 'hidden';
  1004. }
  1005. } else {
  1006. if (method === 0) {
  1007. node.classList.remove('block_obj_none');
  1008. } else if (method === 1) {
  1009. node.classList.remove('block_obj_hidden');
  1010. } else if (method === 2) {
  1011. node.style.display = '';
  1012. } else if (method === 3) {
  1013. node.style.visibility = '';
  1014. }
  1015. }
  1016. }
  1017. }
  1018. };