Block_Obj

BLOCK_OBJ

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

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/407543/830395/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.4.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: 500px;
  26. z-index: 99999;
  27. }
  28. .block_obj_show_wrap {
  29. display: block;
  30. right: 0;
  31. }
  32. .block_obj_hidden_wrap {
  33. right: -510px;
  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. }
  48. .block_obj_checkbox_li {
  49. display: inline-block;
  50. margin-top: 5px;
  51. }
  52. .block_obj_checkbox_input {
  53. clip: rect(0, 0, 0, 0);
  54. position: absolute;
  55. }
  56. .block_obj_checkbox_label {
  57. cursor: pointer;
  58. vertical-align: middle;
  59. }
  60. .block_obj_checkbox_input + label::before {
  61. background-color: silver;
  62. border-radius: 0.1em;
  63. color: #FFF;
  64. content: "\\a0";
  65. display: inline-block;
  66. height: 1em;
  67. line-height: 85%;
  68. margin-right: 0.5em;
  69. text-align: center;
  70. vertical-align: 0.2em;
  71. width: 1em;
  72. }
  73. .block_obj_checkbox_input:checked + label::before {
  74. background-color: #00A1D6;
  75. content: "\\2713";
  76. }
  77. .block_obj_separator_text {
  78. color: #FFB86C;
  79. margin-bottom: 2px;
  80. margin-top: 2px;
  81. }
  82. .block_obj_separator_symbol {
  83. background-color: #303030;
  84. height: 2px;
  85. margin-bottom: 5px;
  86. margin-top: 5px;
  87. min-width: 400px;
  88. }
  89. .block_obj_input_div {
  90. margin-top: 5px;
  91. }
  92. .block_obj_input {
  93. background-color: #C0C0C0;
  94. border: 1px solid #C0C0C0;
  95. color: #000;
  96. font-size: 13px;
  97. min-height: 15px;
  98. margin-left: 5px;
  99. margin-right: 5px;
  100. padding-left: 4px;
  101. }
  102. .block_obj_keyword_input {
  103. width: 150px;
  104. }
  105. .block_obj_input_btn {
  106. background-color: #3da9cc;
  107. border-radius: 3px;
  108. border: 1px solid #73C9E5;
  109. box-shadow: 0 0 4px #73C9E5;
  110. color: #FFF;
  111. cursor: pointer;
  112. display: inline-block;
  113. min-height: 15px;
  114. line-height: normal;
  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. line-height: normal;
  152. }
  153. .block_obj_child_text {
  154. border-right: 1px solid #A9181C;
  155. margin-right: 4px;
  156. padding-right: 4px;
  157. }
  158. .block_obj_child_del {
  159. color: #A9181C;
  160. cursor: pointer;
  161. }
  162. .block_obj_list_textarea_expand {
  163. max-height: 720px;
  164. }
  165. .block_obj_li_hide {
  166. display: none;
  167. }
  168. .block_obj_reg_input {
  169. width: 100px;
  170. }
  171. .block_obj_modifier_input {
  172. width: 50px;
  173. }
  174. .block_obj_button {
  175. background-color: #FB7299;
  176. border-radius: 4px;
  177. border: 1px solid #FB7299;
  178. color: #FFF;
  179. cursor: pointer;
  180. margin-top: 5px;
  181. padding: 2px 4px;
  182. position: relative;
  183. min-height: 17px;
  184. line-height: normal;
  185. }
  186. .block_obj_save_button {
  187. float: right;
  188. margin-right: 5px;
  189. }
  190. .block_obj_cancel_button {
  191. float: left;
  192. margin-left: 5px;
  193. }
  194. .block_obj_expand_box {
  195. bottom: 0px;
  196. height: 6vh;
  197. position: fixed;
  198. right: -6vw;
  199. transition: 0.5s;
  200. width: 12vw;
  201. z-index: 99999;
  202. }
  203. .block_obj_show_expand_box {
  204. right: 0;
  205. width: 6vw;
  206. }
  207. .block_obj_expand_span {
  208. background-color: #00A1D6;
  209. border-radius: 19px;
  210. border: 1px solid #00A1D6;
  211. bottom: 1vh;
  212. color: #FFF;
  213. cursor: pointer;
  214. display: block;
  215. font-size: 13px;
  216. height: 38px;
  217. line-height: 38px;
  218. position: absolute;
  219. right: 1vw;
  220. text-align: center;
  221. width: 38px;
  222. z-index: 99999;
  223. }
  224. .block_obj_expand_span:hover {
  225. box-shadow: 0 0 5px 1px green;
  226. }
  227. .block_obj_move_right {
  228. margin-left: 15px;
  229. }
  230. .block_obj_none {
  231. display: none !important;
  232. }
  233. .block_obj_hidden {
  234. visibility: hidden !important;
  235. }
  236. `;
  237. class Block_Obj {
  238. constructor(config_key, reg_key_array = []) {
  239. this.wrapDiv = null;
  240. this.mainFieldset = null;
  241. this.ulNode = null;
  242. this.style = null;
  243. this.saveField = [];
  244. this.onSave = null;
  245. this.onChange = null;
  246. this.field = [];
  247. this.configKey = config_key;
  248. this.regKeyArray = reg_key_array;
  249. this.config = this.readConfig();
  250. }
  251. init(initialization) {
  252. if (! this.id) {
  253. this.id = initialization.id ? 'blockObj_' + initialization.id : 'blockObj_' + Block_Obj.count;
  254. ! initialization.id && Block_Obj.count ++;
  255. }
  256. this.display = initialization.display ? true : false;
  257. if (initialization.events) {
  258. if (typeof(initialization.events['save']) === 'function') {
  259. this.onSave = initialization.events['save'];
  260. }
  261. if (typeof(initialization.events['change']) === 'function') {
  262. this.onChange = initialization.events['change'];
  263. }
  264. }
  265. this.style = document.createElement('style');
  266. this.style.type = 'text/css';
  267. this.style.innerHTML = BLOCK_STYLE;
  268. document.body.appendChild(this.style);
  269. if (initialization.style && typeof(initialization.style) === 'string') {
  270. let external_style = document.createElement('style');
  271. external_style.type = 'text/css';
  272. external_style.innerHTML = initialization.style;
  273. document.body.appendChild(external_style);
  274. }
  275. ! this.wrapDiv && this.createSettingsPanel();
  276. this.field = initialization.field;
  277. this.settingsPanel();
  278. initialization.menu && typeof(GM_registerMenuCommand) === 'function' && GM_registerMenuCommand(initialization.menu, () => this.expandWrap());
  279. typeof(GM_addValueChangeListener) === 'function' && GM_addValueChangeListener(this.configKey, (_name, _old_value, new_value, remote) => {
  280. if (remote) {
  281. this.destroyAndRebuild(new_value);
  282. typeof(this.onChange) === 'function' && this.onChange(this.getConfig());
  283. }
  284. });
  285. let expand_box = document.createElement('div');
  286. expand_box.className = 'block_obj_expand_box';
  287. expand_box.onmouseenter = function() {
  288. this.classList.add('block_obj_show_expand_box');
  289. };
  290. expand_box.onmouseleave = function() {
  291. this.classList.remove('block_obj_show_expand_box');
  292. };
  293. let expand_span = document.createElement('span');
  294. expand_span.id = this.id + '_expandSpan';
  295. expand_span.className = 'block_obj_expand_span';
  296. expand_span.textContent = '屏蔽';
  297. expand_span.title = '显示屏蔽设置';
  298. expand_span.addEventListener('click', () => this.expandWrap());
  299. expand_box.appendChild(expand_span);
  300. document.body.appendChild(expand_box);
  301. }
  302. settingsPanel() {
  303. if (Array.isArray(this.field)) {
  304. this.field.forEach(ele => {
  305. if (! this.wrapDiv.querySelector('#' + this.id + '_' + (ele.id ? ele.id : ''))) {
  306. switch (ele.type.toLowerCase()) {
  307. case 'separator':
  308. case 's':
  309. this.insertSeparator(ele.id, ele.label, ele.title, ele.classname);
  310. break;
  311. case 'input':
  312. case 'i':
  313. this.insertInput(ele.id, ele.list_id, false, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare);
  314. break;
  315. case 'reg_input':
  316. case 'ri':
  317. this.insertInput(ele.id, ele.list_id, true, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare, ele.modifier_label, ele.modifier_placeholder);
  318. break;
  319. case 'list':
  320. case 'l':
  321. this.insertList(ele.id, (this.config[ele.id] ? this.config[ele.id] : ele.default), ele.label, ele.title, ele.classname);
  322. break;
  323. case 'checkbox':
  324. case 'c':
  325. default:
  326. this.insertCheckbox(ele.id, ele.label, ele.title, (this.config[ele.id] ? this.config[ele.id] : ele.default), ele.classname, ele.move_right);
  327. break;
  328. }
  329. }
  330. });
  331. }
  332. let save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '保存并关闭', '保存设置并关闭设置窗口', e => {
  333. this.expandWrap();
  334. this.saveConfig();
  335. });
  336. let only_save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '仅保存', '仅保存设置', e => {
  337. only_save_button.textContent = '已保存';
  338. save_timer && window.clearTimeout(save_timer);
  339. var save_timer = window.setTimeout(() => {
  340. only_save_button.textContent = '仅保存';
  341. }, 1000);
  342. this.saveConfig();
  343. });
  344. let cancel_button = this.createSpanBtn('block_obj_button block_obj_cancel_button', '取消', '关闭设置窗口', e => {
  345. this.expandWrap();
  346. cancel_timer && window.clearTimeout(cancel_timer);
  347. var cancel_timer = window.setTimeout(() => {
  348. this.display = false;
  349. this.destroyAndRebuild();
  350. }, 800);
  351. e.stopPropagation();
  352. });
  353. this.ulNode.appendChild(save_button);
  354. this.ulNode.appendChild(only_save_button);
  355. this.ulNode.appendChild(cancel_button);
  356. document.body.appendChild(this.wrapDiv);
  357. }
  358. getConfig() {
  359. let real_config = {};
  360. Object.assign(real_config, this.config);
  361. this.regKeyArray.forEach(key => {
  362. real_config[key] = this.convertArray(real_config[key]);
  363. });
  364. return real_config;
  365. }
  366. readConfig() {
  367. let config = {};
  368. if (typeof(GM_getValue) === 'function' && this.configKey) {
  369. config = GM_getValue(this.configKey, {});
  370. }
  371. return config;
  372. }
  373. saveConfig() {
  374. this.saveField.forEach(item => {
  375. if (item.type == 'checkbox') {
  376. this.config[item.key] = document.getElementById(this.id + '_' + item.key).checked;
  377. } else if (item.type == 'list') {
  378. this.config[item.key] = this.extractList(this.id + '_' + item.key);
  379. }
  380. typeof(GM_setValue) && GM_setValue(this.configKey, this.config);
  381. typeof(this.onSave) === 'function' && this.onSave(this.getConfig());
  382. });
  383. }
  384. createSettingsPanel() {
  385. this.wrapDiv = document.createElement('div');
  386. this.wrapDiv.id = this.id + '_wrapDiv';
  387. this.wrapDiv.className = 'block_obj_wrap_div ' + (this.display ? 'block_obj_show_wrap' : 'block_obj_hidden_wrap');
  388. this.mainFieldset = document.createElement('fieldset');
  389. this.mainFieldset.id = this.id + '_mainFieldset';
  390. this.mainFieldset.className = 'block_obj_main_fieldset';
  391. this.wrapDiv.appendChild(this.mainFieldset);
  392. this.ulNode = document.createElement('ul');
  393. this.ulNode.id = this.id + '_ulNode';
  394. this.ulNode.className = 'block_obj_ul_node';
  395. this.mainFieldset.appendChild(this.ulNode);
  396. document.body.appendChild(this.wrapDiv);
  397. }
  398. destroyAndRebuild(new_config = null) {
  399. this.config = new_config || this.config;
  400. document.body.removeChild(this.wrapDiv);
  401. this.createSettingsPanel();
  402. this.settingsPanel();
  403. }
  404. expandWrap() {
  405. let panel = document.getElementById(this.id + '_wrapDiv');
  406. let button = document.getElementById(this.id + '_expandSpan');
  407. if (panel) {
  408. if (panel.classList.contains('block_obj_show_wrap')) {
  409. this.display = false;
  410. panel.classList.remove('block_obj_show_wrap');
  411. panel.classList.add('block_obj_hidden_wrap');
  412. if (button) {
  413. button.title = '显示屏蔽设置';
  414. }
  415. } else {
  416. this.display = true;
  417. panel.classList.remove('block_obj_hidden_wrap');
  418. panel.classList.add('block_obj_show_wrap');
  419. if (button) {
  420. button.title = '隐藏屏蔽设置';
  421. }
  422. }
  423. }
  424. }
  425. insertCheckbox(id, label = '', title = '', checked = false, classname = null, move_right = false) {
  426. let checkbox_li = document.createElement('li');
  427. checkbox_li.className = 'block_obj_checkbox_li';
  428. classname && checkbox_li.classList.add(classname);
  429. move_right && checkbox_li.classList.add('block_obj_move_right');
  430. let checkbox_input = document.createElement('input');
  431. checkbox_input.type = 'checkbox';
  432. checkbox_input.className = 'block_obj_checkbox_input';
  433. checkbox_input.id = this.id + '_' + id;
  434. checkbox_input.checked = checked ? true : false;
  435. let checkbox_label = document.createElement('label');
  436. checkbox_label.className = 'block_obj_checkbox_label';
  437. checkbox_label.setAttribute('for', this.id + '_' + id);
  438. checkbox_label.textContent = label;
  439. checkbox_label.title = title;
  440. checkbox_li.appendChild(checkbox_input);
  441. checkbox_li.appendChild(checkbox_label);
  442. this.ulNode.appendChild(checkbox_li);
  443. this.saveField.push({
  444. 'key': id,
  445. 'type': 'checkbox'
  446. });
  447. }
  448. insertSeparator(id = null, label = null, title = null, li_classname = null) {
  449. let separator_li = document.createElement('li');
  450. separator_li.className = 'block_obj_separator_li';
  451. li_classname && separator_li.classList.add('block_obj_' + li_classname);
  452. let separator_div = document.createElement('div');
  453. if (id) {
  454. separator_div.id = this.id + '_' + id;
  455. }
  456. if (label) {
  457. separator_div.className = 'block_obj_separator_text';
  458. separator_div.textContent = label;
  459. } else {
  460. separator_div.className = 'block_obj_separator_symbol';
  461. }
  462. separator_div.title = title ? title : '';
  463. separator_li.appendChild(separator_div);
  464. this.ulNode.appendChild(separator_li);
  465. }
  466. insertInput(id, list_id, is_reg = false, label = '', title = '', placeholder = '', li_classname = null, campare = null, modifier_label = '', modifier_placeholder = '') {
  467. let input_li = document.createElement('li');
  468. input_li.className = li_classname ? 'block_obj_' + li_classname : '';
  469. let input_div = document.createElement('div');
  470. input_div.className = 'block_obj_input_div';
  471. let input_span = document.createElement('span');
  472. input_span.className = 'block_obj_input_span';
  473. input_span.textContent = label;
  474. input_div.appendChild(input_span);
  475. let input = document.createElement('input');
  476. input.id = this.id + '_' + id;
  477. input.title = title;
  478. input.placeholder = placeholder;
  479. input.type = 'text';
  480. input.className = is_reg ? 'block_obj_input block_obj_reg_input' : 'block_obj_input block_obj_keyword_input';
  481. input_span.appendChild(input);
  482. let modifier_span = document.createElement('span');
  483. modifier_span.textContent = modifier_label;
  484. is_reg && input_div.appendChild(modifier_span);
  485. let modifier_input = document.createElement('input');
  486. modifier_input.placeholder = modifier_placeholder;
  487. modifier_input.type = 'text';
  488. modifier_input.className = 'block_obj_input block_obj_modifier_input';
  489. modifier_span.appendChild(modifier_input);
  490. list_id = this.id + '_' + list_id;
  491. input.addEventListener('keyup', e => {
  492. if (e.keyCode === 13) {
  493. is_reg ? this.addRegListItem(input, modifier_input, list_id) : this.addListItem(input, list_id, campare);
  494. }
  495. });
  496. modifier_input.addEventListener('keyup', e => {
  497. e.keyCode === 13 && this.addRegListItem(input, modifier_input, list_id);
  498. });
  499. let add_btn = this.createSpanBtn('block_obj_input_btn', '添加', '添加内容到列表中', e => {
  500. if (is_reg ? this.addRegListItem(input, modifier_input, list_id) : this.addListItem(input, list_id, campare))
  501. this.buttonClicked(add_btn, '添加成功', 'block_obj_button_clicked');
  502. });
  503. let delete_btn = this.createSpanBtn('block_obj_input_btn', '删除', '从列表中删除符合的项目', e => {
  504. if (is_reg ? this.delRegListItem(input, modifier_input, list_id) : this.delListItem(input, list_id))
  505. this.buttonClicked(delete_btn, '删除成功', 'block_obj_button_clicked');
  506. });
  507. let clear_btn = this.createSpanBtn('block_obj_input_btn', '清空', '清空列表', e => {
  508. document.getElementById(list_id).innerHTML = '';
  509. this.buttonClicked(clear_btn, '清除成功', 'block_obj_button_clicked');
  510. });
  511. let copy_btn = this.createSpanBtn('block_obj_input_btn', '复制', '复制列表', e => {
  512. if (typeof(GM_setClipboard) == 'function') {
  513. GM_setClipboard(this.extractList(list_id).toString(), 'text');
  514. this.buttonClicked(copy_btn, '复制成功', 'block_obj_button_clicked');
  515. } else {
  516. alert('GM_setClipboard is not an interface.');
  517. }
  518. });
  519. let expand_btn = this.createSpanBtn('block_obj_input_btn', '展开', '展开列表', e => {
  520. li_classname && this.toggleList('block_obj_' + li_classname);
  521. if (expand_btn.textContent == '展开') {
  522. expand_btn.textContent = '恢复';
  523. expand_btn.title = '收缩列表';
  524. } else {
  525. expand_btn.textContent = '展开';
  526. expand_btn.title = '展开列表';
  527. }
  528. });
  529. input_div.appendChild(add_btn);
  530. input_div.appendChild(delete_btn);
  531. input_div.appendChild(clear_btn);
  532. input_div.appendChild(copy_btn);
  533. input_div.appendChild(expand_btn);
  534. input_li.appendChild(input_div);
  535. this.ulNode.appendChild(input_li);
  536. }
  537. insertList(id, save_array = [], label = '', title = '', li_classname = '') {
  538. let list_li = document.createElement('li');
  539. list_li.className = li_classname ? 'block_obj_' + li_classname : '';
  540. let list_div = document.createElement('div');
  541. list_div.className = 'block_obj_list_div';
  542. list_div.textContent = label;
  543. list_div.title = title;
  544. let list_textarea_div = document.createElement('div');
  545. list_textarea_div.id = this.id + '_' + id;
  546. list_textarea_div.className = 'block_obj_list_textarea_div';
  547. for (let item of save_array) {
  548. item && list_textarea_div.insertAdjacentElement('afterbegin', this.createListItem(item));
  549. }
  550. list_div.appendChild(list_textarea_div);
  551. list_li.appendChild(list_div);
  552. this.ulNode.appendChild(list_li);
  553. this.saveField.push({
  554. 'key': id,
  555. 'type': 'list'
  556. });
  557. }
  558. addListItem(input, list_id, campare = null) {
  559. let text_value = input.value;
  560. if (text_value) {
  561. let status = true;
  562. if (typeof(campare) === 'function') {
  563. let temp_status = campare(text_value);
  564. if (typeof(temp_status) === 'boolean') {
  565. status = temp_status;
  566. }
  567. }
  568. if (status) {
  569. let text_arr = this.stringToArray(text_value);
  570. let save_arr = this.extractList(list_id);
  571. text_arr.forEach(item => {
  572. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  573. });
  574. input.value = '';
  575. return true;
  576. }
  577. }
  578. return false;
  579. }
  580. delListItem(input, list_id) {
  581. let text_value = input.value;
  582. if (text_value) {
  583. let del_status = false;
  584. let text_arr = this.stringToArray(text_value);
  585. let save_arr = this.extractList(list_id);
  586. text_arr.forEach(item => {
  587. if (save_arr.includes(item)) {
  588. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  589. try {
  590. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(item)]);
  591. del_status = true;
  592. } catch(e) {
  593. del_status = false;
  594. console.error('Block_Obj: Error deleting element.');
  595. console.error(e);
  596. }
  597. }
  598. });
  599. if (del_status) {
  600. input.value = '';
  601. return true;
  602. }
  603. }
  604. return false;
  605. }
  606. addRegListItem(reg_input, modifier_input, list_id) {
  607. let reg_value = reg_input.value;
  608. let modifier_value = modifier_input.value;
  609. if (reg_value) {
  610. try {
  611. let reg_obj = new RegExp(reg_value, modifier_value);
  612. let save_arr = this.extractList(list_id);
  613. ! save_arr.includes(reg_obj.toString()) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(reg_obj.toString()));
  614. reg_input.value = '';
  615. modifier_input.value = '';
  616. return true;
  617. } catch(e) {
  618. console.error('Block_Obj: Invalid regular expression.');
  619. console.error(e);
  620. }
  621. }
  622. return false;
  623. }
  624. delRegListItem(reg_input, modifier_input, list_id) {
  625. let reg_value = reg_input.value;
  626. let modifier_value = modifier_input.value;
  627. if (reg_value) {
  628. let del_status = false;
  629. try {
  630. let reg_obj = new RegExp(reg_value, modifier_value);
  631. let save_arr = this.extractList(list_id);
  632. if (save_arr.includes(reg_obj.toString())) {
  633. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  634. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(reg_obj.toString())]);
  635. del_status = true;
  636. }
  637. } catch(e) {
  638. del_status = false;
  639. console.error('Block_Obj: Invalid regular expression or error deleting element.');
  640. console.error(e);
  641. }
  642. if (del_status) {
  643. reg_input.value = '';
  644. modifier_input.value = '';
  645. return true;
  646. }
  647. return false;
  648. }
  649. }
  650. createSpanBtn(classname, label, title, callback) {
  651. let btn_span = document.createElement('span');
  652. btn_span.className = classname;
  653. btn_span.textContent = label;
  654. btn_span.title = title;
  655. btn_span.addEventListener('click', e => typeof(callback) === 'function' && callback(e));
  656. return btn_span;
  657. }
  658. createListItem(text_value) {
  659. let child_span = document.createElement('span');
  660. child_span.className = 'block_obj_child_span';
  661. let text_span = document.createElement('span');
  662. text_span.className = 'block_obj_child_text';
  663. text_span.textContent = text_value.length > 9 ? text_value.slice(0, 3) + '...' + text_value.slice(-3) : text_value;
  664. if (text_value.length > 9) {
  665. text_span.title = text_value;
  666. }
  667. let del_span = document.createElement('span');
  668. del_span.textContent = 'X';
  669. del_span.title = '移除';
  670. del_span.className = 'block_obj_child_del';
  671. del_span.addEventListener('click', e => child_span.remove());
  672. child_span.appendChild(text_span);
  673. child_span.appendChild(del_span);
  674. return child_span;
  675. }
  676. buttonClicked(button, click_title, click_class) {
  677. let original_title = button.title;
  678. button.title = click_title;
  679. click_class && button.classList.add(click_class);
  680. timer && window.clearTimeout(timer);
  681. var timer = window.setTimeout(() => {
  682. button.title = original_title;
  683. button.classList.remove(click_class);
  684. }, 1000);
  685. }
  686. extractList(list_id) {
  687. let re_arr = [];
  688. let list_dom = document.getElementById(list_id);
  689. let list_arr = list_dom.getElementsByClassName('block_obj_child_text');
  690. for (let i = list_arr.length - 1; i >= 0; i --) {
  691. list_arr[i].title ? re_arr.push(list_arr[i].title) : re_arr.push(list_arr[i].textContent);
  692. }
  693. return re_arr;
  694. }
  695. stringToArray(text_string) {
  696. let temp_array = text_string.split(',');
  697. let return_array = [];
  698. for (let i = 0, l = temp_array.length; i < l; i ++) {
  699. for (let j = i + 1; j < l; j ++) {
  700. if (temp_array[i] === temp_array[j]) {
  701. ++ i;
  702. j = i;
  703. }
  704. }
  705. return_array.push(temp_array[i]);
  706. }
  707. return return_array;
  708. }
  709. toggleList(li_classname) {
  710. for (let li of this.ulNode.querySelectorAll('li')) {
  711. if (li.classList.contains(li_classname)) {
  712. let list_textarea_div = li.querySelector('.block_obj_list_textarea_div');
  713. if (list_textarea_div) {
  714. 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');
  715. }
  716. } else {
  717. li.classList.contains('block_obj_li_hide') ? li.classList.remove('block_obj_li_hide') : li.classList.add('block_obj_li_hide');
  718. }
  719. }
  720. }
  721. convertArray(string_array) {
  722. let re_arr = [];
  723. if (Array.isArray(string_array)) {
  724. for (let i in string_array) {
  725. try {
  726. let new_reg = new RegExp(string_array[i].replace(/^\/|\/[a-z]*$/gi, ''), string_array[i].replace(/^\/.*\/[^a-z]*/i, ''));
  727. re_arr.push(new_reg);
  728. } catch(e) {
  729. console.error('Block_Obj: The transformation contains invalid regular expressions.');
  730. console.error(e);
  731. }
  732. }
  733. }
  734. return re_arr;
  735. }
  736. }
  737. Block_Obj.count = 0;
  738. Block_Obj.fn = {
  739. 'hideOperation': (node, hide_status, method) => {
  740. if (node && node instanceof HTMLElement) {
  741. if (hide_status) {
  742. if (method === 0) {
  743. node.classList.add('block_obj_none');
  744. } else if (method === 1) {
  745. node.classList.add('block_obj_hidden');
  746. } else if (method === 2) {
  747. node.style.display = 'none';
  748. } else if (method === 3) {
  749. node.style.visibility = 'hidden';
  750. }
  751. } else {
  752. if (method === 0) {
  753. node.classList.remove('block_obj_none');
  754. } else if (method === 1) {
  755. node.classList.remove('block_obj_hidden');
  756. } else if (method === 2) {
  757. node.style.display = '';
  758. } else if (method === 3) {
  759. node.style.visibility = '';
  760. }
  761. }
  762. }
  763. }
  764. };