Block_Obj

BLOCK_OBJ

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/407543/830368/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.2.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. `;
  231. class Block_Obj {
  232. constructor(config_key, reg_key_array = []) {
  233. this.wrapDiv = null;
  234. this.mainFieldset = null;
  235. this.ulNode = null;
  236. this.style = null;
  237. this.saveField = [];
  238. this.onSave = null;
  239. this.onChange = null;
  240. this.field = [];
  241. this.configKey = config_key;
  242. this.regKeyArray = reg_key_array;
  243. this.config = this.readConfig();
  244. }
  245. init(initialization) {
  246. if (! this.id) {
  247. this.id = initialization.id ? 'blockObj_' + initialization.id : 'blockObj_' + Block_Obj.count;
  248. ! initialization.id && Block_Obj.count ++;
  249. }
  250. this.display = initialization.display ? true : false;
  251. if (initialization.events) {
  252. if (typeof(initialization.events['save']) === 'function') {
  253. this.onSave = initialization.events['save'];
  254. }
  255. if (typeof(initialization.events['change']) === 'function') {
  256. this.onChange = initialization.events['change'];
  257. }
  258. }
  259. this.style = document.createElement('style');
  260. this.style.type = 'text/css';
  261. this.style.innerHTML = BLOCK_STYLE;
  262. document.body.appendChild(this.style);
  263. if (initialization.style && typeof(initialization.style) === 'string') {
  264. let external_style = document.createElement('style');
  265. external_style.type = 'text/css';
  266. external_style.innerHTML = initialization.style;
  267. document.body.appendChild(external_style);
  268. }
  269. ! this.wrapDiv && this.createSettingsPanel();
  270. this.field = initialization.field;
  271. this.settingsPanel();
  272. initialization.menu && typeof(GM_registerMenuCommand) === 'function' && GM_registerMenuCommand(initialization.menu, () => this.expandWrap());
  273. typeof(GM_addValueChangeListener) === 'function' && GM_addValueChangeListener(this.configKey, (name, old_value, new_value, remote) => {
  274. if (remote) {
  275. this.destroyAndRebuild(new_value);
  276. typeof(this.onChange) === 'function' && this.onChange(this.getConfig());
  277. }
  278. });
  279. let expand_box = document.createElement('div');
  280. expand_box.className = 'block_obj_expand_box';
  281. expand_box.onmouseenter = function() {
  282. this.classList.add('block_obj_show_expand_box');
  283. };
  284. expand_box.onmouseleave = function() {
  285. this.classList.remove('block_obj_show_expand_box');
  286. };
  287. let expand_span = document.createElement('span');
  288. expand_span.id = this.id + '_expandSpan';
  289. expand_span.className = 'block_obj_expand_span';
  290. expand_span.textContent = '屏蔽';
  291. expand_span.title = '显示屏蔽设置';
  292. expand_span.addEventListener('click', () => this.expandWrap());
  293. expand_box.appendChild(expand_span);
  294. document.body.appendChild(expand_box);
  295. }
  296. settingsPanel() {
  297. if (Array.isArray(this.field)) {
  298. this.field.forEach(ele => {
  299. if (! this.wrapDiv.querySelector('#' + this.id + '_' + (ele.id ? ele.id : ''))) {
  300. switch (ele.type.toLowerCase()) {
  301. case 'separator':
  302. case 's':
  303. this.insertSeparator(ele.id, ele.label, ele.title, ele.classname);
  304. break;
  305. case 'input':
  306. case 'i':
  307. this.insertInput(ele.id, ele.list_id, false, ele.label, ele.title, ele.placeholder, ele.classname);
  308. break;
  309. case 'reg_input':
  310. case 'ri':
  311. this.insertInput(ele.id, ele.list_id, true, ele.label, ele.title, ele.placeholder, ele.classname, ele.modifier_label, ele.modifier_placeholder,);
  312. break;
  313. case 'list':
  314. case 'l':
  315. this.insertList(ele.id, (this.config[ele.id] ? this.config[ele.id] : ele.default), ele.label, ele.title, ele.classname);
  316. break;
  317. case 'checkbox':
  318. case 'c':
  319. default:
  320. this.insertCheckbox(ele.id, ele.label, ele.title, (this.config[ele.id] ? this.config[ele.id] : ele.default), ele.classname, ele.move_right);
  321. break;
  322. }
  323. }
  324. });
  325. }
  326. let save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '保存并关闭', '保存设置并关闭设置窗口', e => {
  327. this.expandWrap();
  328. this.saveConfig();
  329. });
  330. let only_save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '仅保存', '仅保存设置', e => {
  331. only_save_button.textContent = '已保存';
  332. save_timer && window.clearTimeout(save_timer);
  333. var save_timer = window.setTimeout(() => {
  334. only_save_button.textContent = '仅保存';
  335. }, 1000);
  336. this.saveConfig();
  337. });
  338. let cancel_button = this.createSpanBtn('block_obj_button block_obj_cancel_button', '取消', '关闭设置窗口', e => {
  339. this.expandWrap();
  340. cancel_timer && window.clearTimeout(cancel_timer);
  341. var cancel_timer = window.setTimeout(() => {
  342. this.display = false;
  343. this.destroyAndRebuild();
  344. }, 800);
  345. e.stopPropagation();
  346. });
  347. this.ulNode.appendChild(save_button);
  348. this.ulNode.appendChild(only_save_button);
  349. this.ulNode.appendChild(cancel_button);
  350. document.body.appendChild(this.wrapDiv);
  351. }
  352. getConfig() {
  353. let real_config = {};
  354. Object.assign(real_config, this.config);
  355. this.regKeyArray.forEach(key => {
  356. real_config[key] = this.convertArray(real_config[key]);
  357. });
  358. return real_config;
  359. }
  360. readConfig() {
  361. let config = {};
  362. if (typeof(GM_getValue) === 'function' && this.configKey) {
  363. config = GM_getValue(this.configKey, {});
  364. }
  365. return config;
  366. }
  367. saveConfig() {
  368. this.saveField.forEach(item => {
  369. if (item.type == 'checkbox') {
  370. this.config[item.key] = document.getElementById(this.id + '_' + item.key).checked;
  371. } else if (item.type == 'list') {
  372. this.config[item.key] = this.extractList(this.id + '_' + item.key);
  373. }
  374. typeof(GM_setValue) && GM_setValue(this.configKey, this.config);
  375. typeof(this.onSave) === 'function' && this.onSave(this.getConfig());
  376. });
  377. }
  378. createSettingsPanel() {
  379. this.wrapDiv = document.createElement('div');
  380. this.wrapDiv.id = this.id + '_wrapDiv';
  381. this.wrapDiv.className = 'block_obj_wrap_div ' + (this.display ? 'block_obj_show_wrap' : 'block_obj_hidden_wrap');
  382. this.mainFieldset = document.createElement('fieldset');
  383. this.mainFieldset.id = this.id + '_mainFieldset';
  384. this.mainFieldset.className = 'block_obj_main_fieldset';
  385. this.wrapDiv.appendChild(this.mainFieldset);
  386. this.ulNode = document.createElement('ul');
  387. this.ulNode.id = this.id + '_ulNode';
  388. this.ulNode.className = 'block_obj_ul_node';
  389. this.mainFieldset.appendChild(this.ulNode);
  390. document.body.appendChild(this.wrapDiv);
  391. }
  392. destroyAndRebuild(new_config = null) {
  393. this.config = new_config || this.config;
  394. document.body.removeChild(this.wrapDiv);
  395. this.createSettingsPanel();
  396. this.settingsPanel();
  397. }
  398. expandWrap() {
  399. let panel = document.getElementById(this.id + '_wrapDiv');
  400. let button = document.getElementById(this.id + '_expandSpan');
  401. if (panel) {
  402. if (panel.classList.contains('block_obj_show_wrap')) {
  403. this.display = false;
  404. panel.classList.remove('block_obj_show_wrap');
  405. panel.classList.add('block_obj_hidden_wrap');
  406. if (button) {
  407. button.title = '显示屏蔽设置';
  408. }
  409. } else {
  410. this.display = true;
  411. panel.classList.remove('block_obj_hidden_wrap');
  412. panel.classList.add('block_obj_show_wrap');
  413. if (button) {
  414. button.title = '隐藏屏蔽设置';
  415. }
  416. }
  417. }
  418. }
  419. insertCheckbox(id, label = '', title = '', checked = false, classname = null, move_right = false) {
  420. let checkbox_li = document.createElement('li');
  421. checkbox_li.className = 'block_obj_checkbox_li';
  422. classname && checkbox_li.classList.add(classname);
  423. move_right && checkbox_li.classList.add('block_obj_move_right');
  424. let checkbox_input = document.createElement('input');
  425. checkbox_input.type = 'checkbox';
  426. checkbox_input.className = 'block_obj_checkbox_input';
  427. checkbox_input.id = this.id + '_' + id;
  428. checkbox_input.checked = checked ? true : false;
  429. let checkbox_label = document.createElement('label');
  430. checkbox_label.className = 'block_obj_checkbox_label';
  431. checkbox_label.setAttribute('for', this.id + '_' + id);
  432. checkbox_label.textContent = label;
  433. checkbox_label.title = title;
  434. checkbox_li.appendChild(checkbox_input);
  435. checkbox_li.appendChild(checkbox_label);
  436. this.ulNode.appendChild(checkbox_li);
  437. this.saveField.push({
  438. 'key': id,
  439. 'type': 'checkbox'
  440. });
  441. }
  442. insertSeparator(id = null, label = null, title = null, li_classname = null) {
  443. let separator_li = document.createElement('li');
  444. separator_li.className = 'block_obj_separator_li';
  445. li_classname && separator_li.classList.add('block_obj_' + li_classname);
  446. let separator_div = document.createElement('div');
  447. if (id) {
  448. separator_div.id = this.id + '_' + id;
  449. }
  450. if (label) {
  451. separator_div.className = 'block_obj_separator_text';
  452. separator_div.textContent = label;
  453. } else {
  454. separator_div.className = 'block_obj_separator_symbol';
  455. }
  456. separator_div.title = title ? title : '';
  457. separator_li.appendChild(separator_div);
  458. this.ulNode.appendChild(separator_li);
  459. }
  460. insertInput(id, list_id, is_reg = false, label = '', title = '', placeholder = '', li_classname = null, modifier_label = '', modifier_placeholder = '') {
  461. let input_li = document.createElement('li');
  462. input_li.className = li_classname ? 'block_obj_' + li_classname : '';
  463. let input_div = document.createElement('div');
  464. input_div.className = 'block_obj_input_div';
  465. let input_span = document.createElement('span');
  466. input_span.className = 'block_obj_input_span';
  467. input_span.textContent = label;
  468. input_div.appendChild(input_span);
  469. let input = document.createElement('input');
  470. input.id = this.id + '_' + id;
  471. input.title = title;
  472. input.placeholder = placeholder;
  473. input.type = 'text';
  474. input.className = is_reg ? 'block_obj_input block_obj_reg_input' : 'block_obj_input block_obj_keyword_input';
  475. input_span.appendChild(input);
  476. let modifier_span = document.createElement('span');
  477. modifier_span.textContent = modifier_label;
  478. is_reg && input_div.appendChild(modifier_span);
  479. let modifier_input = document.createElement('input');
  480. modifier_input.placeholder = modifier_placeholder;
  481. modifier_input.type = 'text';
  482. modifier_input.className = 'block_obj_input block_obj_modifier_input';
  483. modifier_span.appendChild(modifier_input);
  484. list_id = this.id + '_' + list_id;
  485. input.addEventListener('keyup', e => {
  486. if (e.keyCode === 13) {
  487. is_reg ? this.addRegListItem(input, modifier_input, list_id) : this.addListItem(input, list_id);
  488. }
  489. });
  490. modifier_input.addEventListener('keyup', e => {
  491. e.keyCode === 13 && this.addRegListItem(input, modifier_input, list_id);
  492. });
  493. let add_btn = this.createSpanBtn('block_obj_input_btn', '添加', '添加内容到列表中', e => {
  494. if (is_reg ? this.addRegListItem(input, modifier_input, list_id) : this.addListItem(input, list_id))
  495. this.buttonClicked(add_btn, '添加成功', 'block_obj_button_clicked');
  496. });
  497. let delete_btn = this.createSpanBtn('block_obj_input_btn', '删除', '从列表中删除符合的项目', e => {
  498. if (is_reg ? this.delRegListItem(input, modifier_input, list_id) : this.delListItem(input, list_id))
  499. this.buttonClicked(delete_btn, '删除成功', 'block_obj_button_clicked');
  500. });
  501. let clear_btn = this.createSpanBtn('block_obj_input_btn', '清空', '清空列表', e => {
  502. document.getElementById(list_id).innerHTML = '';
  503. this.buttonClicked(clear_btn, '清除成功', 'block_obj_button_clicked');
  504. });
  505. let copy_btn = this.createSpanBtn('block_obj_input_btn', '复制', '复制列表', e => {
  506. if (typeof(GM_setClipboard) == 'function') {
  507. GM_setClipboard(this.extractList(list_id).toString(), 'text');
  508. this.buttonClicked(copy_btn, '复制成功', 'block_obj_button_clicked');
  509. } else {
  510. alert('GM_setClipboard is not an interface.');
  511. }
  512. });
  513. let expand_btn = this.createSpanBtn('block_obj_input_btn', '展开', '展开列表', e => {
  514. li_classname && this.toggleList('block_obj_' + li_classname);
  515. if (expand_btn.textContent == '展开') {
  516. expand_btn.textContent = '恢复';
  517. expand_btn.title = '收缩列表';
  518. } else {
  519. expand_btn.textContent = '展开';
  520. expand_btn.title = '展开列表';
  521. }
  522. });
  523. input_div.appendChild(add_btn);
  524. input_div.appendChild(delete_btn);
  525. input_div.appendChild(clear_btn);
  526. input_div.appendChild(copy_btn);
  527. input_div.appendChild(expand_btn);
  528. input_li.appendChild(input_div);
  529. this.ulNode.appendChild(input_li);
  530. }
  531. insertList(id, save_array = [], label = '', title = '', li_classname = '') {
  532. let list_li = document.createElement('li');
  533. list_li.className = li_classname ? 'block_obj_' + li_classname : '';
  534. let list_div = document.createElement('div');
  535. list_div.className = 'block_obj_list_div';
  536. list_div.textContent = label;
  537. list_div.title = title;
  538. let list_textarea_div = document.createElement('div');
  539. list_textarea_div.id = this.id + '_' + id;
  540. list_textarea_div.className = 'block_obj_list_textarea_div';
  541. for (let item of save_array) {
  542. item && list_textarea_div.insertAdjacentElement('afterbegin', this.createListItem(item));
  543. }
  544. list_div.appendChild(list_textarea_div);
  545. list_li.appendChild(list_div);
  546. this.ulNode.appendChild(list_li);
  547. this.saveField.push({
  548. 'key': id,
  549. 'type': 'list'
  550. });
  551. }
  552. addListItem(input, list_id) {
  553. let text_value = input.value;
  554. if (text_value) {
  555. let text_arr = this.stringToArray(text_value);
  556. let save_arr = this.extractList(list_id);
  557. text_arr.forEach(item => {
  558. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  559. });
  560. input.value = '';
  561. return true;
  562. }
  563. return false;
  564. }
  565. delListItem(input, list_id) {
  566. let text_value = input.value;
  567. if (text_value) {
  568. let del_status = false;
  569. let text_arr = this.stringToArray(text_value);
  570. let save_arr = this.extractList(list_id);
  571. text_arr.forEach(item => {
  572. if (save_arr.includes(item)) {
  573. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  574. try {
  575. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(item)]);
  576. del_status = true;
  577. } catch(e) {
  578. del_status = false;
  579. console.error('Block_Obj: Error deleting element.');
  580. console.error(e);
  581. }
  582. }
  583. });
  584. if (del_status) {
  585. input.value = '';
  586. return true;
  587. }
  588. }
  589. return false;
  590. }
  591. addRegListItem(reg_input, modifier_input, list_id) {
  592. let reg_value = reg_input.value;
  593. let modifier_value = modifier_input.value;
  594. if (reg_value) {
  595. try {
  596. let reg_obj = new RegExp(reg_value, modifier_value);
  597. let save_arr = this.extractList(list_id);
  598. ! save_arr.includes(reg_obj.toString()) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(reg_obj.toString()));
  599. reg_input.value = '';
  600. modifier_input.value = '';
  601. return true;
  602. } catch(e) {
  603. console.error('Block_Obj: Invalid regular expression.');
  604. console.error(e);
  605. }
  606. }
  607. return false;
  608. }
  609. delRegListItem(reg_input, modifier_input, list_id) {
  610. let reg_value = reg_input.value;
  611. let modifier_value = modifier_input.value;
  612. if (reg_value) {
  613. let del_status = false;
  614. try {
  615. let reg_obj = new RegExp(reg_value, modifier_value);
  616. let save_arr = this.extractList(list_id);
  617. if (save_arr.includes(reg_obj.toString())) {
  618. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  619. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(reg_obj.toString())]);
  620. del_status = true;
  621. }
  622. } catch(e) {
  623. del_status = false;
  624. console.error('Block_Obj: Invalid regular expression or error deleting element.');
  625. console.error(e);
  626. }
  627. if (del_status) {
  628. reg_input.value = '';
  629. modifier_input.value = '';
  630. return true;
  631. }
  632. return false;
  633. }
  634. }
  635. createSpanBtn(classname, label, title, callback) {
  636. let btn_span = document.createElement('span');
  637. btn_span.className = classname;
  638. btn_span.textContent = label;
  639. btn_span.title = title;
  640. btn_span.addEventListener('click', e => typeof(callback) === 'function' && callback(e));
  641. return btn_span;
  642. }
  643. createListItem(text_value) {
  644. let child_span = document.createElement('span');
  645. child_span.className = 'block_obj_child_span';
  646. let text_span = document.createElement('span');
  647. text_span.className = 'block_obj_child_text';
  648. text_span.textContent = text_value.length > 9 ? text_value.slice(0, 3) + '...' + text_value.slice(-3) : text_value;
  649. if (text_value.length > 9) {
  650. text_span.title = text_value;
  651. }
  652. let del_span = document.createElement('span');
  653. del_span.textContent = 'X';
  654. del_span.title = '移除';
  655. del_span.className = 'block_obj_child_del';
  656. del_span.addEventListener('click', e => child_span.remove());
  657. child_span.appendChild(text_span);
  658. child_span.appendChild(del_span);
  659. return child_span;
  660. }
  661. buttonClicked(button, click_title, click_class) {
  662. let original_title = button.title;
  663. button.title = click_title;
  664. click_class && button.classList.add(click_class);
  665. timer && window.clearTimeout(timer);
  666. var timer = window.setTimeout(() => {
  667. button.title = original_title;
  668. button.classList.remove(click_class);
  669. }, 1000);
  670. }
  671. extractList(list_id) {
  672. let re_arr = [];
  673. let list_dom = document.getElementById(list_id);
  674. let list_arr = list_dom.getElementsByClassName('block_obj_child_text');
  675. for (let i = list_arr.length - 1; i >= 0; i --) {
  676. list_arr[i].title ? re_arr.push(list_arr[i].title) : re_arr.push(list_arr[i].textContent);
  677. }
  678. return re_arr;
  679. }
  680. stringToArray(text_string) {
  681. let temp_array = text_string.split(',');
  682. let return_array = [];
  683. for (let i = 0, l = temp_array.length; i < l; i ++) {
  684. for (let j = i + 1; j < l; j ++) {
  685. if (temp_array[i] === temp_array[j]) {
  686. ++ i;
  687. j = i;
  688. }
  689. }
  690. return_array.push(temp_array[i]);
  691. }
  692. return return_array;
  693. }
  694. toggleList(li_classname) {
  695. for (let li of this.ulNode.querySelectorAll('li')) {
  696. if (li.classList.contains(li_classname)) {
  697. let list_textarea_div = li.querySelector('.block_obj_list_textarea_div');
  698. if (list_textarea_div) {
  699. 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');
  700. }
  701. } else {
  702. li.classList.contains('block_obj_li_hide') ? li.classList.remove('block_obj_li_hide') : li.classList.add('block_obj_li_hide');
  703. }
  704. }
  705. }
  706. convertArray(string_array) {
  707. let re_arr = [];
  708. if (Array.isArray(string_array)) {
  709. for (let i in string_array) {
  710. try {
  711. let new_reg = new RegExp(string_array[i].replace(/^\/|\/[a-z]*$/gi, ''), string_array[i].replace(/^\/.*\/[^a-z]*/i, ''));
  712. re_arr.push(new_reg);
  713. } catch(e) {
  714. console.error('Block_Obj: The transformation contains invalid regular expressions.');
  715. console.error(e);
  716. }
  717. }
  718. }
  719. return re_arr;
  720. }
  721. }
  722. Block_Obj.count = 0;