Greasy Fork 还支持 简体中文。

Block_Obj

BLOCK_OBJ

目前為 2020-08-24 提交的版本,檢視 最新版本

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