Block_Obj

BLOCK_OBJ

当前为 2020-08-01 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/407543/832768/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.8.1
  6. // @description BLOCK_OBJ
  7. // @author pana
  8. // @license GNU General Public License v3.0 or later
  9. // ==/UserScript==
  10.  
  11. const BLOCK_STYLE = `
  12. .block_obj_wrap_div {
  13. background-color: #222222;
  14. border-radius: 3px;
  15. border: 1px solid #282A36;
  16. bottom: 6vh;
  17. box-shadow: 0 0 5px #282A36;
  18. color: #D3D3D3;
  19. font-size: 13px;
  20. margin: 0px;
  21. padding: 0px;
  22. position: fixed;
  23. text-align: left;
  24. transition: 0.8s;
  25. width: 520px;
  26. z-index: 99999;
  27. }
  28. .block_obj_show_wrap {
  29. display: block;
  30. right: 0;
  31. }
  32. .block_obj_hidden_wrap {
  33. right: -530px;
  34. }
  35. .block_obj_main_fieldset {
  36. border-radius: 3px;
  37. border: 3px groove #00A1D6;
  38. height: auto;
  39. margin: 8px;
  40. min-width: 300px;
  41. padding: 4px 9px 6px 9px;
  42. width: auto;
  43. }
  44. .block_obj_ul_node {
  45. list-style: none;
  46. padding-left: 0;
  47. margin: 0;
  48. }
  49. .block_obj_checkbox_li {
  50. display: inline-block;
  51. margin-top: 5px;
  52. }
  53. .block_obj_checkbox_input {
  54. clip: rect(0, 0, 0, 0);
  55. position: absolute;
  56. }
  57. .block_obj_checkbox_label {
  58. cursor: pointer;
  59. vertical-align: middle;
  60. }
  61. .block_obj_checkbox_input + label::before {
  62. background-color: silver;
  63. border-radius: 0.1em;
  64. color: #FFF;
  65. content: "\\a0";
  66. display: inline-block;
  67. height: 1em;
  68. line-height: 85%;
  69. margin-right: 0.5em;
  70. text-align: center;
  71. vertical-align: 0.2em;
  72. width: 1em;
  73. }
  74. .block_obj_checkbox_input:checked + label::before {
  75. background-color: #00A1D6;
  76. content: "\\2713";
  77. }
  78. .block_obj_separator_text {
  79. color: #FFB86C;
  80. margin-top: 5px;
  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. margin-left: 5px;
  115. text-align: center;
  116. vertical-align: bottom;
  117. white-space: nowrap;
  118. width: 30px;
  119. }
  120. .block_obj_list_div {
  121. margin-top: 5px;
  122. }
  123. .block_obj_list_textarea_div {
  124. border: 1px dotted #00A1D6;
  125. margin-top: 3px;
  126. max-height: 60px;
  127. min-height: 3px;
  128. overflow: auto;
  129. }
  130. .block_obj_list_textarea_div::-webkit-scrollbar {
  131. background-color: #979797;
  132. border-radius: 5px;
  133. width: 10px;
  134. }
  135. .block_obj_list_textarea_div::-webkit-scrollbar-thumb {
  136. background-color: #404040;
  137. border-radius: 5px;
  138. }
  139. .block_obj_button_clicked {
  140. color: #000;
  141. }
  142. .block_obj_child_span {
  143. background-color: #3D3D3D;
  144. border-radius: 5px;
  145. border: 1px solid #3D3D3D;
  146. display: inline-block;
  147. margin: 3px;
  148. padding: 2px;
  149. min-height: 18px;
  150. }
  151. .block_obj_child_text {
  152. border-right: 1px solid #A9181C;
  153. margin-right: 4px;
  154. padding-right: 4px;
  155. }
  156. .block_obj_child_del {
  157. color: #A9181C;
  158. cursor: pointer;
  159. }
  160. .block_obj_list_textarea_expand {
  161. max-height: 720px;
  162. }
  163. .block_obj_li_hide {
  164. display: none;
  165. }
  166. .block_obj_reg_input {
  167. width: 100px;
  168. }
  169. .block_obj_modifier_input {
  170. width: 50px;
  171. }
  172. .block_obj_button {
  173. background-color: #FB7299;
  174. border-radius: 4px;
  175. border: 1px solid #FB7299;
  176. color: #FFF;
  177. cursor: pointer;
  178. margin-top: 5px;
  179. padding: 2px 4px;
  180. position: relative;
  181. min-height: 17px;
  182. }
  183. .block_obj_save_button {
  184. float: right;
  185. margin-right: 5px;
  186. }
  187. .block_obj_cancel_button {
  188. float: left;
  189. margin-left: 5px;
  190. }
  191. .block_obj_expand_box {
  192. bottom: 0px;
  193. height: 6vh;
  194. position: fixed;
  195. right: -6vw;
  196. transition: 0.5s;
  197. width: 12vw;
  198. z-index: 99999;
  199. }
  200. .block_obj_show_expand_box {
  201. right: 0;
  202. width: 6vw;
  203. }
  204. .block_obj_expand_span {
  205. background-color: #00A1D6;
  206. border-radius: 19px;
  207. border: 1px solid #00A1D6;
  208. bottom: 1vh;
  209. color: #FFF;
  210. cursor: pointer;
  211. display: block;
  212. font-size: 13px;
  213. height: 38px;
  214. line-height: 38px;
  215. position: absolute;
  216. right: 1vw;
  217. text-align: center;
  218. width: 38px;
  219. z-index: 99999;
  220. }
  221. .block_obj_expand_span:hover {
  222. box-shadow: 0 0 5px 1px green;
  223. }
  224. .block_obj_move_right {
  225. margin-left: 15px;
  226. }
  227. .block_obj_none {
  228. display: none !important;
  229. }
  230. .block_obj_hidden {
  231. visibility: hidden !important;
  232. }
  233. .block_obj_presentation_div {
  234. display: flex;
  235. position: fixed;
  236. background-color: rgba(0, 0, 0, .5);
  237. top: 0;
  238. bottom: 0;
  239. left: 0;
  240. right: 0;
  241. z-index: 100000;
  242. align-items: center;
  243. justify-content: center;
  244. }
  245. .block_obj_dialog_div {
  246. position: relative;
  247. width: 400px;
  248. background-color: #4e5654;
  249. border: 0 solid #000;
  250. border-radius: 12px;
  251. display: flex;
  252. flex-direction: column;
  253. }
  254. .block_obj_big_bang_top_part {
  255. margin-top: 5px;
  256. margin-bottom: 5px;
  257. }
  258. .block_obj_big_bang_h3 {
  259. font-size: 20px;
  260. color: #fff;
  261. margin-left: 5px;
  262. }
  263. .block_obj_big_bang_deselect_btn {
  264. float: right;
  265. margin-top: 5px;
  266. margin-right: 5px;
  267. color: #2a2a92;
  268. cursor: pointer;
  269. }
  270. .block_obj_big_bang_middle_part {
  271. margin-top: 5px;
  272. margin-bottom: 5px;
  273. max-height: 500px;
  274. overflow-y: auto;
  275. }
  276. .block_obj_big_bang_text {
  277. background-color: #3e3939;
  278. color: #fff;
  279. padding: 5px;
  280. margin: 4px;
  281. font-size: 16px;
  282. display: inline-block;
  283. border-radius: 99px;
  284. cursor: pointer;
  285. }
  286. .block_obj_big_bang_text_selected {
  287. background-color: #3636b1 !important;
  288. }
  289. .block_obj_big_bang_bottom_part {
  290. margin-top: 5px;
  291. margin-bottom: 5px;
  292. text-align: center;
  293. }
  294. .block_obj_big_bang_add_btn, .block_obj_big_bang_copy_btn {
  295. display: inline-block;
  296. padding: 10px;
  297. color: #fff;
  298. background-color: #325561;
  299. cursor: pointer;
  300. border-radius: 99px;
  301. margin-left: 20px;
  302. margin-right: 20px;
  303. }
  304. .block_obj_big_bang_add_btn:hover, .block_obj_big_bang_copy_btn:hover {
  305. color: #2a2a92;
  306. }
  307. `;
  308. class Block_Obj {
  309. constructor(config_key, reg_key_array = []) {
  310. this.wrapDiv = null;
  311. this.mainFieldset = null;
  312. this.ulNode = null;
  313. this.style = null;
  314. this.saveField = [];
  315. this.onSave = null;
  316. this.onChange = null;
  317. this.field = [];
  318. this.configKey = config_key;
  319. this.regKeyArray = reg_key_array;
  320. this.config = this.readConfig();
  321. }
  322. init(initialization) {
  323. if (! this.id) {
  324. this.id = initialization.id ? 'blockObj_' + initialization.id : 'blockObj_' + Block_Obj.count;
  325. ! initialization.id && Block_Obj.count ++;
  326. }
  327. this.display = initialization.display ? true : false;
  328. if (initialization.events) {
  329. if (typeof(initialization.events['save']) === 'function') {
  330. this.onSave = initialization.events['save'];
  331. }
  332. if (typeof(initialization.events['change']) === 'function') {
  333. this.onChange = initialization.events['change'];
  334. }
  335. }
  336. this.style = document.createElement('style');
  337. this.style.type = 'text/css';
  338. this.style.innerHTML = BLOCK_STYLE;
  339. document.body.appendChild(this.style);
  340. if (initialization.style && typeof(initialization.style) === 'string') {
  341. let external_style = document.createElement('style');
  342. external_style.type = 'text/css';
  343. external_style.innerHTML = initialization.style;
  344. document.body.appendChild(external_style);
  345. }
  346. ! this.wrapDiv && this.createSettingsPanel();
  347. this.field = initialization.field;
  348. this.settingsPanel();
  349. initialization.menu && typeof(GM_registerMenuCommand) === 'function' && GM_registerMenuCommand(initialization.menu, () => this.expandWrap());
  350. typeof(GM_addValueChangeListener) === 'function' && GM_addValueChangeListener(this.configKey, (_name, _old_value, new_value, remote) => {
  351. if (remote) {
  352. this.destroyAndRebuild(new_value);
  353. typeof(this.onChange) === 'function' && this.onChange(this.getConfig());
  354. }
  355. });
  356. let expand_box = document.createElement('div');
  357. expand_box.className = 'block_obj_expand_box';
  358. expand_box.onmouseenter = function() {
  359. this.classList.add('block_obj_show_expand_box');
  360. };
  361. expand_box.onmouseleave = function() {
  362. this.classList.remove('block_obj_show_expand_box');
  363. };
  364. let hover_button = {};
  365. if (initialization.hover_button) {
  366. Object.assign(hover_button, initialization.hover_button);
  367. }
  368. let expand_span = document.createElement('span');
  369. expand_span.id = this.id + '_expandSpan';
  370. expand_span.className = 'block_obj_expand_span';
  371. expand_span.textContent = hover_button.label == null ? '屏蔽' : hover_button.label;
  372. expand_span.title = hover_button.title == null ? '显示/隐藏屏蔽设置' : hover_button.title;
  373. expand_span.addEventListener('click', () => {
  374. this.expandWrap();
  375. if (hover_button.click && typeof(hover_button.click) === 'function') {
  376. hover_button.click(expand_span);
  377. }
  378. });
  379. expand_box.appendChild(expand_span);
  380. document.body.appendChild(expand_box);
  381. }
  382. settingsPanel() {
  383. if (Array.isArray(this.field)) {
  384. this.field.forEach(ele => {
  385. if (! this.wrapDiv.querySelector('#' + this.id + '_' + (ele.id ? ele.id : ''))) {
  386. switch (ele.type.toLowerCase()) {
  387. case 'separator':
  388. case 's':
  389. this.insertSeparator(ele.id, ele.label, ele.title, ele.classname);
  390. break;
  391. case 'br':
  392. case 'b':
  393. this.insertBr(ele.classname);
  394. break;
  395. case 'input':
  396. case 'i':
  397. this.insertInput(ele.list_id, ele.id, false, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare);
  398. break;
  399. case 'reg_input':
  400. case 'ri':
  401. this.insertInput(ele.list_id, ele.id, true, ele.label, ele.title, ele.placeholder, ele.classname, ele.campare, ele.modifier_label, ele.modifier_placeholder);
  402. break;
  403. case 'list':
  404. case 'l':
  405. this.insertList(ele.id, (this.config[ele.id] == null ? ele.default : this.config[ele.id]), ele.label, ele.title, ele.classname);
  406. break;
  407. case 'checkbox':
  408. case 'c':
  409. default:
  410. this.insertCheckbox(ele.id, ele.label, ele.title, (this.config[ele.id] == null ? ele.default : this.config[ele.id]), ele.classname, ele.move_right);
  411. break;
  412. }
  413. }
  414. });
  415. }
  416. let save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '保存并关闭', '保存设置并关闭设置窗口', e => {
  417. this.expandWrap();
  418. this.saveConfig();
  419. });
  420. let only_save_button = this.createSpanBtn('block_obj_button block_obj_save_button', '仅保存', '仅保存设置', e => {
  421. only_save_button.textContent = '已保存';
  422. save_timer && window.clearTimeout(save_timer);
  423. var save_timer = window.setTimeout(() => {
  424. only_save_button.textContent = '仅保存';
  425. }, 1000);
  426. this.saveConfig();
  427. });
  428. let cancel_button = this.createSpanBtn('block_obj_button block_obj_cancel_button', '取消', '关闭设置窗口', e => {
  429. this.expandWrap();
  430. cancel_timer && window.clearTimeout(cancel_timer);
  431. var cancel_timer = window.setTimeout(() => {
  432. this.display = false;
  433. this.destroyAndRebuild();
  434. }, 800);
  435. e.stopPropagation();
  436. });
  437. this.ulNode.appendChild(save_button);
  438. this.ulNode.appendChild(only_save_button);
  439. this.ulNode.appendChild(cancel_button);
  440. document.body.appendChild(this.wrapDiv);
  441. }
  442. getConfig() {
  443. let real_config = {};
  444. Object.assign(real_config, this.config);
  445. this.regKeyArray.forEach(key => {
  446. real_config[key] = this.convertArray(real_config[key]);
  447. });
  448. return real_config;
  449. }
  450. readConfig() {
  451. let config = {};
  452. if (typeof(GM_getValue) === 'function' && this.configKey) {
  453. config = GM_getValue(this.configKey, {});
  454. }
  455. return config;
  456. }
  457. saveConfig() {
  458. this.saveField.forEach(item => {
  459. if (item.type == 'checkbox') {
  460. this.config[item.key] = document.getElementById(this.id + '_' + item.key).checked;
  461. } else if (item.type == 'list') {
  462. this.config[item.key] = this.extractList(this.id + '_' + item.key);
  463. }
  464. });
  465. typeof(GM_setValue) && GM_setValue(this.configKey, this.config);
  466. typeof(this.onSave) === 'function' && this.onSave(this.getConfig());
  467. }
  468. createSettingsPanel() {
  469. this.wrapDiv = document.createElement('div');
  470. this.wrapDiv.id = this.id + '_wrapDiv';
  471. this.wrapDiv.className = 'block_obj_wrap_div ' + (this.display ? 'block_obj_show_wrap' : 'block_obj_hidden_wrap');
  472. this.mainFieldset = document.createElement('fieldset');
  473. this.mainFieldset.id = this.id + '_mainFieldset';
  474. this.mainFieldset.className = 'block_obj_main_fieldset';
  475. this.wrapDiv.appendChild(this.mainFieldset);
  476. this.ulNode = document.createElement('ul');
  477. this.ulNode.id = this.id + '_ulNode';
  478. this.ulNode.className = 'block_obj_ul_node';
  479. this.mainFieldset.appendChild(this.ulNode);
  480. document.body.appendChild(this.wrapDiv);
  481. }
  482. destroyAndRebuild(new_config = null) {
  483. this.config = new_config || this.config;
  484. document.body.removeChild(this.wrapDiv);
  485. this.createSettingsPanel();
  486. this.settingsPanel();
  487. }
  488. expandWrap() {
  489. let panel = document.getElementById(this.id + '_wrapDiv');
  490. if (panel) {
  491. if (panel.classList.contains('block_obj_show_wrap')) {
  492. this.display = false;
  493. panel.classList.remove('block_obj_show_wrap');
  494. panel.classList.add('block_obj_hidden_wrap');
  495. } else {
  496. this.display = true;
  497. panel.classList.remove('block_obj_hidden_wrap');
  498. panel.classList.add('block_obj_show_wrap');
  499. }
  500. }
  501. }
  502. insertCheckbox(id, label = '', title = '', checked = false, classname = null, move_right = false) {
  503. let checkbox_li = document.createElement('li');
  504. checkbox_li.className = 'block_obj_checkbox_li';
  505. classname && checkbox_li.classList.add(classname);
  506. move_right && checkbox_li.classList.add('block_obj_move_right');
  507. let checkbox_input = document.createElement('input');
  508. checkbox_input.type = 'checkbox';
  509. checkbox_input.className = 'block_obj_checkbox_input';
  510. checkbox_input.id = this.id + '_' + id;
  511. checkbox_input.checked = checked ? true : false;
  512. let checkbox_label = document.createElement('label');
  513. checkbox_label.className = 'block_obj_checkbox_label';
  514. checkbox_label.setAttribute('for', this.id + '_' + id);
  515. checkbox_label.textContent = label;
  516. checkbox_label.title = title;
  517. checkbox_li.appendChild(checkbox_input);
  518. checkbox_li.appendChild(checkbox_label);
  519. this.ulNode.appendChild(checkbox_li);
  520. this.saveField.push({
  521. 'key': id,
  522. 'type': 'checkbox'
  523. });
  524. }
  525. insertSeparator(id = null, label = null, title = null, li_classname = null) {
  526. let separator_li = document.createElement('li');
  527. separator_li.className = 'block_obj_separator_li';
  528. li_classname && separator_li.classList.add('block_obj_' + li_classname);
  529. let separator_div = document.createElement('div');
  530. if (id) {
  531. separator_div.id = this.id + '_' + id;
  532. }
  533. if (label) {
  534. separator_div.className = 'block_obj_separator_text';
  535. separator_div.textContent = label;
  536. } else {
  537. separator_div.className = 'block_obj_separator_symbol';
  538. }
  539. separator_div.title = title ? title : '';
  540. separator_li.appendChild(separator_div);
  541. this.ulNode.appendChild(separator_li);
  542. }
  543. insertBr(classname = null) {
  544. let br = document.createElement('br');
  545. br.className = classname ? classname : '';
  546. this.ulNode.appendChild(br);
  547. }
  548. insertInput(list_id, id = null, is_reg = false, label = '', title = '', placeholder = '', li_classname = null, campare = null, modifier_label = '', modifier_placeholder = '') {
  549. let input_li = document.createElement('li');
  550. input_li.className = li_classname ? 'block_obj_' + li_classname : '';
  551. let input_div = document.createElement('div');
  552. input_div.className = 'block_obj_input_div';
  553. let input_span = document.createElement('span');
  554. input_span.className = 'block_obj_input_span';
  555. input_span.textContent = label;
  556. input_div.appendChild(input_span);
  557. let input = document.createElement('input');
  558. if (id) {
  559. input.id = this.id + '_' + id;
  560. }
  561. input.title = title;
  562. input.placeholder = placeholder;
  563. input.type = 'text';
  564. input.className = is_reg ? 'block_obj_input block_obj_reg_input' : 'block_obj_input block_obj_keyword_input';
  565. input_span.appendChild(input);
  566. let modifier_span = document.createElement('span');
  567. modifier_span.textContent = modifier_label;
  568. is_reg && input_div.appendChild(modifier_span);
  569. let modifier_input = document.createElement('input');
  570. if (id) {
  571. modifier_input.id = this.id + '_modifier_' + id;
  572. }
  573. modifier_input.placeholder = modifier_placeholder;
  574. modifier_input.type = 'text';
  575. modifier_input.className = 'block_obj_input block_obj_modifier_input';
  576. modifier_span.appendChild(modifier_input);
  577. let the_list_id = this.id + '_' + list_id;
  578. input.addEventListener('keyup', e => {
  579. if (e.keyCode === 13) {
  580. is_reg ? this.addRegListItem(input, modifier_input, the_list_id) : this.addListItem(input, the_list_id, campare);
  581. }
  582. });
  583. modifier_input.addEventListener('keyup', e => {
  584. e.keyCode === 13 && this.addRegListItem(input, modifier_input, the_list_id);
  585. });
  586. let add_btn = this.createSpanBtn('block_obj_input_btn', '添加', '添加内容到列表中', e => {
  587. if (is_reg ? this.addRegListItem(input, modifier_input, the_list_id) : this.addListItem(input, the_list_id, campare)) {
  588. this.buttonClicked(add_btn, '添加成功', 'block_obj_button_clicked');
  589. }
  590. });
  591. let delete_btn = this.createSpanBtn('block_obj_input_btn', '删除', '从列表中删除符合的项目', e => {
  592. if (is_reg ? this.delRegListItem(input, modifier_input, the_list_id) : this.delListItem(input, the_list_id)) {
  593. this.buttonClicked(delete_btn, '删除成功', 'block_obj_button_clicked');
  594. }
  595. });
  596. let clear_btn = this.createSpanBtn('block_obj_input_btn', '清空', '清空列表', e => {
  597. document.getElementById(the_list_id).innerHTML = '';
  598. this.buttonClicked(clear_btn, '清除成功', 'block_obj_button_clicked');
  599. });
  600. let copy_btn = this.createSpanBtn('block_obj_input_btn', '复制', '复制列表', e => {
  601. if (typeof(GM_setClipboard) === 'function') {
  602. GM_setClipboard(this.extractList(the_list_id).toString(), 'text');
  603. this.buttonClicked(copy_btn, '复制成功', 'block_obj_button_clicked');
  604. } else {
  605. alert('GM_setClipboard is not an interface.');
  606. }
  607. });
  608. let expand_btn = this.createSpanBtn('block_obj_input_btn', '展开', '展开列表', e => {
  609. li_classname && this.toggleList('block_obj_' + li_classname);
  610. if (expand_btn.textContent == '展开') {
  611. expand_btn.textContent = '恢复';
  612. expand_btn.title = '收缩列表';
  613. } else {
  614. expand_btn.textContent = '展开';
  615. expand_btn.title = '展开列表';
  616. }
  617. });
  618. input_div.appendChild(add_btn);
  619. input_div.appendChild(delete_btn);
  620. input_div.appendChild(clear_btn);
  621. input_div.appendChild(copy_btn);
  622. input_div.appendChild(expand_btn);
  623. input_li.appendChild(input_div);
  624. this.ulNode.appendChild(input_li);
  625. }
  626. insertList(id, save_array = [], label = '', title = '', li_classname = '') {
  627. let list_li = document.createElement('li');
  628. list_li.className = li_classname ? 'block_obj_' + li_classname : '';
  629. let list_div = document.createElement('div');
  630. list_div.className = 'block_obj_list_div';
  631. list_div.textContent = label;
  632. list_div.title = title;
  633. let list_textarea_div = document.createElement('div');
  634. list_textarea_div.id = this.id + '_' + id;
  635. list_textarea_div.className = 'block_obj_list_textarea_div';
  636. for (let item of save_array) {
  637. item && list_textarea_div.insertAdjacentElement('afterbegin', this.createListItem(item));
  638. }
  639. list_div.appendChild(list_textarea_div);
  640. list_li.appendChild(list_div);
  641. this.ulNode.appendChild(list_li);
  642. this.saveField.push({
  643. 'key': id,
  644. 'type': 'list'
  645. });
  646. }
  647. addListItem(input, list_id, campare = null) {
  648. let text_value = input.value;
  649. if (text_value) {
  650. let text_arr = this.stringToArray(text_value);
  651. let save_arr = this.extractList(list_id);
  652. text_arr.forEach(item => {
  653. let status = true;
  654. if (typeof(campare) === 'function') {
  655. let temp_status = campare(item);
  656. if (typeof(temp_status) === 'boolean') {
  657. status = temp_status;
  658. }
  659. }
  660. if (status) {
  661. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  662. }
  663. });
  664. input.value = '';
  665. return true;
  666. }
  667. return false;
  668. }
  669. delListItem(input, list_id) {
  670. let text_value = input.value;
  671. if (text_value) {
  672. let del_status = false;
  673. let text_arr = this.stringToArray(text_value);
  674. let save_arr = this.extractList(list_id);
  675. text_arr.forEach(item => {
  676. if (save_arr.includes(item)) {
  677. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  678. try {
  679. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(item)]);
  680. del_status = true;
  681. } catch(e) {
  682. del_status = false;
  683. console.error('Block_Obj: Error deleting element.');
  684. console.error(e);
  685. }
  686. }
  687. });
  688. if (del_status) {
  689. input.value = '';
  690. return true;
  691. }
  692. }
  693. return false;
  694. }
  695. addRegListItem(reg_input, modifier_input, list_id) {
  696. let reg_value = reg_input.value;
  697. let modifier_value = modifier_input.value;
  698. if (reg_value) {
  699. try {
  700. let reg_obj = new RegExp(reg_value, modifier_value);
  701. let save_arr = this.extractList(list_id);
  702. ! save_arr.includes(reg_obj.toString()) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(reg_obj.toString()));
  703. reg_input.value = '';
  704. modifier_input.value = '';
  705. return true;
  706. } catch(e) {
  707. console.error('Block_Obj: Invalid regular expression.');
  708. console.error(e);
  709. }
  710. }
  711. return false;
  712. }
  713. delRegListItem(reg_input, modifier_input, list_id) {
  714. let reg_value = reg_input.value;
  715. let modifier_value = modifier_input.value;
  716. if (reg_value) {
  717. let del_status = false;
  718. try {
  719. let reg_obj = new RegExp(reg_value, modifier_value);
  720. let save_arr = this.extractList(list_id);
  721. if (save_arr.includes(reg_obj.toString())) {
  722. let total_child = document.getElementById(list_id).getElementsByClassName('block_obj_child_span');
  723. document.getElementById(list_id).removeChild(total_child[total_child.length - 1 - save_arr.indexOf(reg_obj.toString())]);
  724. del_status = true;
  725. }
  726. } catch(e) {
  727. del_status = false;
  728. console.error('Block_Obj: Invalid regular expression or error deleting element.');
  729. console.error(e);
  730. }
  731. if (del_status) {
  732. reg_input.value = '';
  733. modifier_input.value = '';
  734. return true;
  735. }
  736. return false;
  737. }
  738. }
  739. createSpanBtn(classname, label, title, callback) {
  740. let btn_span = this.createBasicBtn('span', label, title, classname);
  741. btn_span.addEventListener('click', e => typeof(callback) === 'function' && callback(e));
  742. return btn_span;
  743. }
  744. createListItem(text_value) {
  745. let child_span = document.createElement('span');
  746. child_span.className = 'block_obj_child_span';
  747. let text_span = document.createElement('span');
  748. text_span.className = 'block_obj_child_text';
  749. text_span.textContent = text_value.length > 9 ? text_value.slice(0, 3) + '...' + text_value.slice(-3) : text_value;
  750. if (text_value.length > 9) {
  751. text_span.title = text_value;
  752. }
  753. let del_span = document.createElement('span');
  754. del_span.textContent = 'X';
  755. del_span.title = '移除';
  756. del_span.className = 'block_obj_child_del';
  757. del_span.addEventListener('click', e => child_span.remove());
  758. child_span.appendChild(text_span);
  759. child_span.appendChild(del_span);
  760. return child_span;
  761. }
  762. createBasicBtn(type, text, title, classname) {
  763. let btn_type = 'span';
  764. if (typeof(type) === 'string') {
  765. switch (type.toLowerCase()) {
  766. case 'div':
  767. case 'd':
  768. btn_type = 'div';
  769. break;
  770. case 'a':
  771. btn_type = 'a';
  772. break;
  773. case 'button':
  774. btn_type = 'button';
  775. break;
  776. case 'input':
  777. btn_type = 'input';
  778. break;
  779. case 'i':
  780. btn_type = 'i';
  781. break;
  782. case 'b':
  783. btn_type = 'b';
  784. break;
  785. case 'span':
  786. case 's':
  787. default:
  788. btn_type = 'span';
  789. break;
  790. }
  791. }
  792. let btn = document.createElement(btn_type);
  793. btn.textContent = text ? text : '';
  794. btn.title = title ? title : '';
  795. btn.className = classname ? classname : '';
  796. return btn;
  797. }
  798. createBlockBtn(value, list_id, classname, type = 'span', text = '', title = '', campare = null) {
  799. let block_btn = this.createBasicBtn(type, text, title, classname);
  800. block_btn.addEventListener('click', e => {
  801. e.stopPropagation();
  802. if (value && list_id) {
  803. let status = true;
  804. if (typeof(campare) === 'function') {
  805. let temp_status = campare(value);
  806. if (typeof(temp_status) === 'boolean') {
  807. status = temp_status;
  808. }
  809. }
  810. if (status) {
  811. let the_list_id = this.id + '_' + list_id;
  812. let save_arr = this.extractList(the_list_id);
  813. ! save_arr.includes(value) && document.getElementById(the_list_id) && document.getElementById(the_list_id).insertAdjacentElement('afterbegin', this.createListItem(value));
  814. }
  815. }
  816. this.saveConfig();
  817. });
  818. return block_btn;
  819. }
  820. createBigBangBtn(value, list_id, classname, type = 'span', text = '', title = '', campare = null) {
  821. let big_bang_btn = this.createBasicBtn(type, text, title, classname);
  822. big_bang_btn.addEventListener('click', e => {
  823. e.stopPropagation();
  824. if (value && list_id) {
  825. let the_list_id = this.id + '_' + list_id;
  826. this.injectionBigBangPanel(value, the_list_id, campare);
  827. }
  828. });
  829. return big_bang_btn;
  830. }
  831. injectionBigBangPanel(value, list_id, campare = null) {
  832. let presentation_div = document.createElement('div');
  833. presentation_div.id = this.id + '_presentationDiv';
  834. presentation_div.className = 'block_obj_presentation_div';
  835. presentation_div.addEventListener('click', function(event) {
  836. if (event.target === this) {
  837. if (presentation_div) {
  838. presentation_div.remove();
  839. }
  840. }
  841. });
  842. let dialog_div = document.createElement('div');
  843. dialog_div.className = 'block_obj_dialog_div';
  844. presentation_div.appendChild(dialog_div);
  845. let top_part = document.createElement('div');
  846. top_part.className = 'block_obj_big_bang_top_part';
  847. let h3 = document.createElement('h3');
  848. h3.className = 'block_obj_big_bang_h3';
  849. h3.textContent = '大爆炸';
  850. top_part.appendChild(h3);
  851. let deselect_btn = this.createSpanBtn('block_obj_big_bang_deselect_btn', '取消选择', '取消全部已选择的内容', e => {
  852. e.stopPropagation();
  853. let select_arr = document.querySelectorAll('.block_obj_big_bang_text_selected');
  854. for (let s of select_arr) {
  855. s.classList.remove('block_obj_big_bang_text_selected');
  856. }
  857. });
  858. top_part.appendChild(deselect_btn);
  859. let value_array = value.replace(/\s| /gi, '').split('');
  860. let middle_part = document.createElement('div');
  861. middle_part.className = 'block_obj_big_bang_middle_part';
  862. let node_array = value_array.map((ele, index) => {
  863. let ele_node = this.createBasicBtn('span', ele, '', 'block_obj_big_bang_text');
  864. ele_node.setAttribute('data-index', index);
  865. ele_node.addEventListener('click', e => {
  866. e.stopPropagation();
  867. ele_node.classList.contains('block_obj_big_bang_text_selected') ? ele_node.classList.remove('block_obj_big_bang_text_selected') : ele_node.classList.add('block_obj_big_bang_text_selected');
  868. });
  869. return ele_node;
  870. });
  871. node_array.forEach(item => {
  872. middle_part.appendChild(item);
  873. });
  874. let bottom_part = document.createElement('div');
  875. bottom_part.className = 'block_obj_big_bang_bottom_part';
  876. let add_btn = this.createSpanBtn('block_obj_big_bang_add_btn', '添加', '添加选择的内容到列表中', e => {
  877. e.stopPropagation();
  878. let text_value = this.getSelectedText('block_obj_big_bang_text_selected');
  879. if (text_value && list_id) {
  880. let text_arr = this.stringToArray(text_value);
  881. let save_arr = this.extractList(list_id);
  882. text_arr.forEach(item => {
  883. let status = true;
  884. if (typeof(campare) === 'function') {
  885. let temp_status = campare(item);
  886. if (typeof(temp_status) === 'boolean') {
  887. status = temp_status;
  888. }
  889. }
  890. if (status) {
  891. ! save_arr.includes(item) && document.getElementById(list_id).insertAdjacentElement('afterbegin', this.createListItem(item));
  892. }
  893. });
  894. this.saveConfig();
  895. presentation_div.remove();
  896. }
  897. });
  898. bottom_part.appendChild(add_btn);
  899. let copy_btn = this.createSpanBtn('block_obj_big_bang_copy_btn', '复制', '复制选择的内容到剪贴板中', e => {
  900. e.stopPropagation();
  901. let text_value = this.getSelectedText('block_obj_big_bang_text_selected');
  902. if (text_value) {
  903. GM_setClipboard(text_value, 'text');
  904. presentation_div.remove();
  905. }
  906. });
  907. bottom_part.appendChild(copy_btn);
  908. dialog_div.appendChild(top_part);
  909. dialog_div.appendChild(middle_part);
  910. dialog_div.appendChild(bottom_part);
  911. document.body.appendChild(presentation_div);
  912. }
  913. getSelectedText(classname) {
  914. let selected_array = document.getElementsByClassName(classname);
  915. let last_index = -1;
  916. let text_value = '';
  917. for (let selected of selected_array) {
  918. let index = selected.getAttribute('data-index') || -1;
  919. if (Number(index) == last_index + 1) {
  920. text_value += selected.textContent;
  921. } else {
  922. text_value = text_value ? text_value + ',' + selected.textContent : selected.textContent;
  923. }
  924. last_index = Number(index);
  925. }
  926. return text_value;
  927. }
  928. buttonClicked(button, click_title, click_class) {
  929. let original_title = button.title;
  930. button.title = click_title;
  931. click_class && button.classList.add(click_class);
  932. timer && window.clearTimeout(timer);
  933. var timer = window.setTimeout(() => {
  934. button.title = original_title;
  935. button.classList.remove(click_class);
  936. }, 1000);
  937. }
  938. extractList(list_id) {
  939. let re_arr = [];
  940. let list_dom = document.getElementById(list_id);
  941. let list_arr = list_dom.getElementsByClassName('block_obj_child_text');
  942. for (let i = list_arr.length - 1; i >= 0; i --) {
  943. list_arr[i].title ? re_arr.push(list_arr[i].title) : re_arr.push(list_arr[i].textContent);
  944. }
  945. return re_arr;
  946. }
  947. stringToArray(text_string) {
  948. let temp_array = text_string.split(',');
  949. let return_array = [];
  950. for (let i = 0, l = temp_array.length; i < l; i ++) {
  951. for (let j = i + 1; j < l; j ++) {
  952. if (temp_array[i] === temp_array[j]) {
  953. ++ i;
  954. j = i;
  955. }
  956. }
  957. return_array.push(temp_array[i]);
  958. }
  959. return return_array;
  960. }
  961. toggleList(li_classname) {
  962. for (let li of this.ulNode.querySelectorAll('li')) {
  963. if (li.classList.contains(li_classname)) {
  964. let list_textarea_div = li.querySelector('.block_obj_list_textarea_div');
  965. if (list_textarea_div) {
  966. 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');
  967. }
  968. } else {
  969. li.classList.contains('block_obj_li_hide') ? li.classList.remove('block_obj_li_hide') : li.classList.add('block_obj_li_hide');
  970. }
  971. }
  972. for (let br of this.ulNode.querySelectorAll('br')) {
  973. br.classList.contains('block_obj_li_hide') ? br.classList.remove('block_obj_li_hide') : br.classList.add('block_obj_li_hide');
  974. }
  975. }
  976. convertArray(string_array) {
  977. let re_arr = [];
  978. if (Array.isArray(string_array)) {
  979. for (let i in string_array) {
  980. try {
  981. let new_reg = new RegExp(string_array[i].replace(/^\/|\/[a-z]*$/gi, ''), string_array[i].replace(/^\/.*\/[^a-z]*/i, ''));
  982. re_arr.push(new_reg);
  983. } catch(e) {
  984. console.error('Block_Obj: The transformation contains invalid regular expressions.');
  985. console.error(e);
  986. }
  987. }
  988. }
  989. return re_arr;
  990. }
  991. }
  992. Block_Obj.count = 0;
  993. Block_Obj.fn = {
  994. 'hideOperation': (node, hide_status, method = 0) => {
  995. if (node && node instanceof HTMLElement) {
  996. if (hide_status) {
  997. if (method === 0) {
  998. node.classList.add('block_obj_none');
  999. } else if (method === 1) {
  1000. node.classList.add('block_obj_hidden');
  1001. } else if (method === 2) {
  1002. node.style.display = 'none';
  1003. } else if (method === 3) {
  1004. node.style.visibility = 'hidden';
  1005. }
  1006. } else {
  1007. if (method === 0) {
  1008. node.classList.remove('block_obj_none');
  1009. } else if (method === 1) {
  1010. node.classList.remove('block_obj_hidden');
  1011. } else if (method === 2) {
  1012. node.style.display = '';
  1013. } else if (method === 3) {
  1014. node.style.visibility = '';
  1015. }
  1016. }
  1017. }
  1018. }
  1019. };