Greasy Fork 还支持 简体中文。

Block_Obj

BLOCK_OBJ

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

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