Greasy Fork 还支持 简体中文。

Block_Obj

BLOCK_OBJ

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

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