Programer's Web Friend

change Programer's Web style to mobile device friendly ,and more readble

目前为 2020-04-08 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Programer's Web Friend
  3. // @namespace https://tampermonkey.net/
  4. // @version 3.1.6
  5. // @description change Programer's Web style to mobile device friendly ,and more readble
  6. // @author @Amormaid
  7. // @run-at document-end
  8. // @match http://*/*
  9. // @include http://*
  10. // @include https://*
  11. // @exclude http://localhost*
  12. // @license MIT License
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16.  
  17. main()
  18.  
  19. function main() {
  20. // console.log('document.cookie', document.cookie)
  21. // document.cookie = document.cookie + 'hhhhhhhhhhhhhhhhhhhhhhh';
  22. // console.log('document.cookie after', document.cookie)
  23. // var root = document.body;
  24. // var hostname = /\d+\.\d+\.\d+\.\d+/.test(window.location.hostname) ? "ip": window.location.hostname;
  25. var hostname = window.location.hostname;
  26.  
  27. if (exclude_check(hostname)) {
  28. return
  29. }
  30.  
  31. // console.clear();
  32.  
  33. try {
  34. console.time("allen_web_time_count");
  35.  
  36. // customer change
  37. let rule = customer_rule(hostname)
  38. rule && rule.action()
  39. // main_content_adjust(obj.main_ele);
  40. // var {main_ele} = main_ele_searcher();
  41. content_purge(main_ele)
  42. // ele_remover(main_content);
  43. // full window
  44. // document.body.style.cssText = "position:absolute; top:0;right:0; bottom:0;left:0;"
  45. // document.body.style.width = window.innerWidth
  46. // document.body.style.height = window.innerHeight
  47. // change_style();
  48. console.timeEnd("allen_web_time_count");
  49. } catch(err) {
  50. //console.log(err.name,' ',err.message);
  51. console.log(err.message);
  52. }
  53. }
  54.  
  55. // white list check
  56. function exclude_check(hostname_param) {
  57. // 不执行脚本的网站白名单
  58. let exclude_list = [
  59. "www.youtube.com",
  60. "demo.mycodes.net",
  61. "kiwivm.64clouds.com",
  62. "www.instagram.com",
  63. "www.h-ui.net",
  64. "www.layui.com",
  65. "www.kixeye.com",
  66. "pan.baidu.com",
  67. "www.rishiqing.com",
  68. "wx.qq.com",
  69. "mail.126.com",
  70. "twitter.com",
  71. "reactjs.org",
  72. "codepen.io",
  73. "free.modao.cc",
  74. "lanhuapp.com",
  75. "developers.weixin.qq.com",
  76. // "jira.vankeservice.com",
  77. "www.tapd.cn",
  78. "modao.cc",
  79. "element-cn.eleme.io",
  80. "github.com",
  81. ""
  82. ]
  83. return exclude_list.includes(hostname_param)
  84. }
  85.  
  86. // 自定义样式调整
  87. function customer_rule(hostname) {
  88. let rules = [
  89. {
  90. rule: "default",
  91. hostname: "ip",
  92. regexp: "",
  93. action: () => {}
  94. },
  95. {
  96. rule: "default",
  97. hostname: "jira.vankeservice.com",
  98. regexp: "",
  99. action: async () => {
  100. if (window.location.href !== 'http://jira.vankeservice.com/browse/COST-677') {
  101. // if (window.location.href !== 'http://jira.vankeservice.com/browse/COST-863') {
  102. console.log('不是项目页,不能填工时')
  103. return
  104. }
  105. await sleep(1000)
  106. const my_data_radio = Array.from(_$('input[type=radio]', "return_all")).find(e => e.parentNode.innerText === 'Only My Data')
  107. console.log('my_data_radio', my_data_radio)
  108. if (!my_data_radio.checked) {
  109. my_data_radio.click()
  110. }
  111. const time_list = Array.from(_$('.sc-hZSUBg.imwLHE', "return_all")).map(e => e.innerText) // .find(e => e.parentNode.innerText === 'Only My Data')
  112. console.log('time_list', time_list)
  113. const now = new Date()
  114. const time_now_string = `${now.getFullYear()}-${('0' + (now.getMonth() + 1)).slice(-2)}-${now.getDate()}`
  115. const today_not_log = !time_list.includes(time_now_string)
  116. // $$('input[type=radio]')
  117. // console.log('button', _$('[name=logTimeBtn]', "return_one"))
  118. // _$('[name=logTimeBtn]', "return_one").click()
  119. // await sleep(1000)
  120. // console.log('button',_$('#from'))
  121.  
  122. // jjjj
  123. const today_should_log = () => {
  124. const national_holiday = [
  125. '2020-04-04',
  126. '2020-04-05',
  127. '2020-04-06',
  128. '2020-05-01',
  129. '2020-05-02',
  130. '2020-05-03',
  131. '2020-05-04',
  132. '2020-05-05',
  133. '2020-06-25',
  134. '2020-06-26',
  135. '2020-06-27',
  136. '2020-10-01',
  137. '2020-10-02',
  138. '2020-10-03',
  139. '2020-10-04',
  140. '2020-10-05',
  141. '2020-10-06',
  142. '2020-10-07',
  143. '2020-10-08'
  144. ]
  145. const national_workday = [
  146. '2020-04-26',
  147. '2020-05-09',
  148. '2020-06-28',
  149. '2020-09-27',
  150. '2020-10-10'
  151. ]
  152. if (national_holiday.includes(getFormatDate(Date.now()))) {
  153. return false
  154. }
  155. if (national_workday.includes(getFormatDate(Date.now()))) {
  156. return true
  157. }
  158. const day_week = new Date(Date.now()).getDay()
  159. if (day_week === 0 || day_week === 6) {
  160. return false
  161. } else {
  162. return true
  163. }
  164. }
  165. const can_log_work_time = today_should_log && today_not_log
  166. if (can_log_work_time){
  167. /*
  168. console.log('button', _$('[name=logTimeBtn]', "return_one"))
  169. const begin_time = `${getFormatDate(Date.now())}T09:00:27.792`
  170. const end_time = `${getFormatDate(Date.now())}T11:00:27.792`
  171. const data = {
  172. "attributes":{},
  173. "billableSeconds":"",
  174. "worker":"xiecy08",
  175. "comment":"编写代码",
  176. "started":begin_time,
  177. "timeSpentSeconds":28800,
  178. "originTaskId":"43581",
  179. "remainingEstimate":0,
  180. "endDate":end_time
  181. }
  182. */
  183. const begin_time = `${getFormatDate(Date.now())} 09:00:24.000`
  184. const update_time = `${getFormatDate(Date.now())} 09:01:24.000`
  185. const data = [{
  186. timeSpent: '1d',
  187. billableSeconds: 28800,
  188. issue: {
  189. estimatedRemainingSeconds: 0,
  190. components: [],
  191. versions: [],
  192. summary: '开发类-2018立项朴韵自营装修项目-朴韵自营装修系统-朴韵自营装修-v2.0',
  193. iconUrl: '/secure/viewavatar?size=xsmall&avatarId=10314&avatarType=issuetype',
  194. projectId: 11200,
  195. projectKey: 'COST',
  196. issueType: 'Time',
  197. issueStatus: 'To Do',
  198. internalIssue: false,
  199. reporterKey: 'adminjira',
  200. key: 'COST-677',
  201. id: 42572
  202. },
  203. timeSpentSeconds: 28800,
  204. comment: '朴韵后台和客户端BUG解决',
  205. tempoWorklogId: 180361,
  206. attributes: {},
  207. worker: 'xiecy08',
  208. updater: 'xiecy08',
  209. started: begin_time,
  210. dateCreated: update_time,
  211. dateUpdated: update_time,
  212. originTaskId: 42572,
  213. originId: 180361
  214. }]
  215. postData('http://jira.vankeservice.com/rest/tempo-timesheets/4/worklogs/', data)
  216. setTimeout(() => {
  217. // window.location.href = window.location.href
  218. }, 3 * 1000)
  219. }
  220. setTimeout(() => {
  221. // window.location.href = window.location.href
  222. }, 8 * 3600 * 1000)
  223. }
  224. },
  225. {
  226. rule: "default",
  227. hostname: "qyapi.weixin.qq.com",
  228. regexp: "",
  229. action: () => {
  230. // test
  231. const poster = (word) => {
  232. fetch('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=dfe3c5f9-1c8e-4498-883d-4c4a721e7957', {
  233. method: 'POST',
  234. body: JSON.stringify({
  235. "msgtype": "text",
  236. "text": {
  237. "content": word
  238. }
  239. }),
  240. headers: {
  241. accept: 'application/json',
  242. 'content-type': 'application/json'
  243. },
  244. mode: 'cors'
  245. })
  246. }
  247. poster('hello world')
  248. }
  249. },
  250. {
  251. rule: "default",
  252. hostname: "wc-kx-vip.sjc.kixeye.com",
  253. regexp: "",
  254. action: () => {
  255. _$('#above-game-message', 'remove_one');
  256. _$('#kxp-footer-bar', 'remove_one');
  257. _$('#topnavbar_back', 'remove_one');
  258. }
  259. },
  260. {
  261. rule: "default",
  262. hostname: ["www.baidu.com", "zhidao.baidu.com"],
  263. regexp: "",
  264. action: () => {
  265. let interval_id = setInterval( () => {
  266. const right_list_length = 5
  267. _$("#content_right", 'remove_all');
  268. var zhidao_body = _$("#body", "return_one");
  269. var container = _$("#container", "return_one");
  270. var head = _$("#head", "return_one");
  271. var pad = _$("#s_tab", "return_one");
  272. _$("#rs", "remove_all")
  273. var u = _$("#u", "return_one");
  274. container.style.cssText = "display:block;width:100%;position:relative;"
  275. head && (() => {
  276. head.style.position = 'relative';
  277. head.style.width = window.innerWidth - 100 + 'px';
  278. head.style['min-width'] = 0;
  279. })();
  280. pad && (() => {
  281. pad.style.padding = '0 0 0 121px'
  282. })();
  283. u && (() => {
  284. u.style.display = 'none'
  285. })();
  286. zhidao_body && (() => {
  287. zhidao_body.style.left = "50px"
  288. })();
  289.  
  290. let origin_length = document.getElementById("content_left").children.length
  291.  
  292. if (origin_length > 6) {
  293. // console.log(content_list)
  294. let right_list = Array.prototype.slice.call(_$(".result", "return_all"),-1 - right_list_length, -1)
  295. let container_r = document.createElement("div")
  296. container_r.style.cssText += "display:block;position:absolute;top:40px;left:740px;width:600px;"
  297. // let str = Array.prototype.reduce.call(right_list, (html_str, item ,index , right_list) => (item.innerHTML + html_str), '')
  298. Array.prototype.forEach.call(right_list, item => {
  299. item.style.cssText += "padding:10px 0;"
  300. container_r.appendChild(item)
  301. })
  302. let container_r_dom = container.appendChild(container_r)
  303. let page_indic = _$("#page", "return_one")
  304. page_indic.style.cssText = "position:absolute;top:0px;left:660px;margin:-60px 0 0 0;z-index:300"
  305. }
  306.  
  307. },300)
  308. }
  309. },
  310. {
  311. rule: "default",
  312. hostname: ["i.taobao.com", "trade.taobao.com", "rate.taobao.com", "buyertrade.taobao.com"],
  313. regexp: "",
  314. action: () => {
  315. _$(".m-guess-you-like", "remove_one");
  316. _$(".m-someone-like-you", 'remove_one');
  317. _$("#p4p_ad", 'remove_one');
  318. _$(".J_guess-you-like", 'remove_one');
  319. }
  320. },
  321. {
  322. rule: "default",
  323. hostname: ["www.cnblogs.com", "blog.csdn.net"],
  324. regexp: "",
  325. action: () => {
  326. _$('.btn-close', 'return_one').click();
  327. _$('#btn-readmore', 'return_one').click();
  328. _$('.recommend-box', 'remove_one')
  329. let main = _$('main', 'return_one')
  330. document.body.style.backgroundImage = 'none';
  331. document.body.style.color = '#fff'
  332. document.body.innerHTML = ''
  333. // document.body.style.cssText = 'background-image: "none" ;'
  334. document.body.appendChild(main)
  335. main.style.cssText = 'width: 900px; float: left; margin: 0 auto 0 120px;'
  336.  
  337. console.log('dasdasda is ', _$('p', 'return_all'))
  338. Array.from(_$('p', 'return_all')).forEach(e => e.style.color = '#fff')
  339.  
  340. /*
  341. setTimeout(() => {
  342. _$('aside', 'remove_one')
  343. _$(".adblock", "remove_one");
  344. },2000)
  345.  
  346. var content = _$("#content", "return_one");
  347. _$("#sidebar", "remove_one");
  348. _$("#side", 'remove_one');
  349.  
  350. content && (() => {
  351. content.style["margin-right"] = "10";
  352. content.style.width = window.innerWidth - 100 + 'px';
  353. })();
  354.  
  355. let article = _$("#article_content", "return_one");
  356. article && (() => {
  357. article.style.height = "100%";
  358. article.style.overflow = "visible";
  359. })()
  360. _$(".recommend-item-box", "remove_all");
  361. _$(".hide-article-box", "remove_one");
  362. _$("aside", "remove_one");
  363. let main = _$("main", "return_one")
  364. if (main) main.style.float = "left";
  365. setTimeout(() => {
  366. _$(".adblock", "remove_one");
  367. },
  368. 3000)
  369. _$(".pulllog-box", "remove_one");
  370. */
  371. }
  372. },
  373. {
  374. rule: "default",
  375. hostname: "www.w3school.com.cn",
  376. regexp: "",
  377. action: () => {
  378. _$("#navsecond", 'hide_one');
  379. }
  380. },
  381. {
  382. rule: "default",
  383. hostname: "wallstreetcn.com",
  384. regexp: "",
  385. action: () => {
  386. setInterval(() => {
  387. _$('.news-item__cover', 'remove_all');
  388. _$('.qn-img', 'remove_all');
  389. // console.log(new Date() - 0)
  390. change_style();
  391. },
  392. 500);
  393.  
  394. let main = _$('main', 'return_one');
  395. document.body.innerHTML = '';
  396. document.body.appendChild(main);
  397.  
  398. _$('.left-bar', 'remove_one');
  399. }
  400. },
  401. {
  402. rule: "default",
  403. hostname: "www.merriam-webster.com",
  404. regexp: "",
  405. action: () => {
  406. _$(".right-rail", 'hide_one');
  407. _$("#recirc-bar-footer", 'hide_one');
  408. _$(".wgt-related-to.jc-card-box.clearfix", 'hide_one');
  409. }
  410. },
  411. {
  412. rule: "default",
  413. hostname: "blog.sina.com.cn",
  414. regexp: "",
  415. action: () => {
  416. _$("#column_1", 'hide_one');
  417. }
  418. },
  419. {
  420. rule: "default",
  421. hostname: ["blog.jobbole.com", "web.jobbole.com"],
  422. regexp: "",
  423. action: () => {
  424. _$("#sidebar", 'hide_one');
  425. var grid = _$(".grid-8", 'return_one');
  426. grid && (() => {
  427. grid.style.width = "100%"
  428. })();
  429. }
  430. },
  431. {
  432. rule: "default",
  433. hostname: "juejin.im",
  434. regexp: "",
  435. action: () => {
  436. var interval_id = setInterval(() => {
  437. var a = _$(".show-full", 'return_one');
  438. var b = _$(".show-full-block", 'return_one');
  439. _$(".show-full-btn", 'remove_one');
  440. _$('.columen-view-aside', 'hide_one');
  441.  
  442. if (a) a.style.height = "auto";
  443. a && a.setAttribute('style', 'height:auto')
  444. if (b) {
  445. clearInterval(interval_id);
  446. b.setAttribute('style', 'height:auto')
  447. }
  448. },
  449. 100);
  450. setInterval(change_style, 300)
  451. }
  452. },
  453. {
  454. rule: "default",
  455. hostname: "www.cnblogs.com",
  456. regexp: "",
  457. action: () => {
  458. _$("#sideBar", 'hide_one');
  459. _$("#vid", 'hide_one');
  460. _$("#left", 'hide_one');
  461. _$("#right_content", 'hide_one');
  462. _$("#leftcontent", 'hide_one');
  463. var a = _$("#centercontent", 'return_one');
  464. if (a) {
  465. a.style['padding-left'] = 0;
  466. }
  467. }
  468. },
  469. {
  470. rule: "default",
  471. hostname: "github.com",
  472. regexp: "",
  473. action: () => {
  474. document.body.style.minWidth = '100px';
  475. var github_pad = document.querySelector('.column.three-fourths.codesearch-results.pr-6');
  476. if (github_pad) {
  477. github_pad.style.padding = '10px 0 10px 30px';
  478. }
  479. }
  480. },
  481. {
  482. rule: "default",
  483. hostname: "wiki.jikexueyuan.com",
  484. regexp: "",
  485. action: () => {
  486. _$(".detail-left", 'hide_one');
  487. var a = _$(".detail-main", 'no_option', 'one');
  488. a.style['margin-left'] = '0px';
  489. a.style.width = document.body.clientWidth + 'px';
  490. }
  491. },
  492. {
  493. rule: "default",
  494. hostname: "www.cssmoban.com",
  495. regexp: "",
  496. action: () => {
  497. var a = _$(".wide-main.col-media-main.clearfix", 'return_one');
  498. a.style.width = document.body.clientWidth + 'px';
  499. }
  500. },
  501. {
  502. rule: "default",
  503. hostname: "www.kancloud.cn",
  504. regexp: "",
  505. action: () => {
  506. var interval_id = setInterval(() => {
  507. var a = _$(".sidebar", 'return_one');
  508. if (a) {
  509. _$(".workspace", 'return_one').style.left = "180px";
  510. a.style.width = '180px';
  511. change_style(a);
  512. clearInterval(interval_id);
  513. }
  514. },
  515. 100);
  516. }
  517. },
  518. {
  519. rule: "default",
  520. hostname: "php.net",
  521. regexp: "",
  522. action: () => {
  523. _$('.layout-menu', 'remove_one');
  524. if (_$(".sect1", 'return_one')) _$(".sect1", 'return_one').style.width = document.body.clientWidth - 50 + "px";
  525. if (_$("#usernotes", 'return_one')) _$("#usernotes", 'return_one').style.width = document.body.clientWidth - 50 + "px";
  526. }
  527. },
  528. {
  529. rule: "default",
  530. hostname: "www.zhihu.com",
  531. regexp: "",
  532. action: () => {
  533. _$('.Question-sideColumn.Question-sideColumn--sticky', 'remove_one');
  534. _$('.AdblockBanner-inner', 'remove_one');
  535. _$('.QuestionHeader-side', 'remove_all');
  536. _$('.AppHeader-userInfo', 'remove_all');
  537.  
  538. var browser_width = document.body.clientWidth - 50 + "px";
  539. _$('.QuestionHeader-content', 'return_one').style.width = browser_width;
  540.  
  541. setInterval(() => {
  542. _$('.HitQrcode', 'remove_one');
  543. },
  544. 100);
  545. }
  546. },
  547. {
  548. rule: "default",
  549. hostname: "www.letscorp.net",
  550. regexp: "",
  551. action: () => {
  552. _$('#commentlist', 'remove_one');
  553. _$('#sidebar', 'remove_one');
  554. _$('#header', 'remove_one');
  555.  
  556. var browser_width = document.body.clientWidth - 50 + "px";
  557. _$('#container', 'return_one').style.width = browser_width;
  558. _$('#main', 'return_one').style.height = "auto";
  559. var p_ele = _$('p', 'return_all');
  560. Array.prototype.forEach.call(p_ele,
  561. function(e) {
  562. e.style["font-size"] = "16px";
  563. });
  564. }
  565. },
  566. {
  567. rule: "default",
  568. hostname: "segmentfault.com",
  569. regexp: "",
  570. action: () => {
  571. _$("#loginBanner", "remove_one");
  572. }
  573. },
  574. {
  575. rule: "default",
  576. hostname: "huziketang.mangojuice.top",
  577. regexp: "",
  578. action: () => {
  579. for (var i = 0; i < 10000; i++) {
  580. clearInterval(i);
  581. }-
  582. _$("div", "return_all").forEach(function(ele) {
  583. if (ele.id && ele.id !== "wrapper" && ele.id !== "uyan_frame" && ele.id !== "donate-mask") {
  584. ele.parentNode.removeChild(ele);
  585. }
  586. });
  587. }
  588. },
  589. {
  590. rule: "default",
  591. hostname: "lvv2.com",
  592. regexp: "",
  593. action: () => {
  594. if (_$(".link.show", "return_all")) {
  595. _$(".link.show", "return_all").forEach(function(ele) {
  596. if (ele.querySelector('a.title')) {
  597. // console.log(ele.querySelector('a.title').innerHTML)
  598. ele.querySelector('a.title').style.fontSize = '16px'
  599. var title = ele.querySelector('a.title').innerHTML
  600. var porn_list = ['日', '勾引', '野外', '肏', '抽插', '公厕', '舔', '茎', '肛', '奴', '约炮', '母狗', '调教', '草', '鸡巴', '小受', '口爆', '野战', 'SM', 'sm', '贱货', '被虐', '淫', '内裤', '屁眼', '蕾丝', '姿势', '体位', '色情', '骚', '屌', '淫荡', '射', '艹', '丝袜', '情色', '调情', '做爱', '操', '捆绑', '挑逗', '绿帽', '潮喷', '援交', 'jj', 'JJ', '潮吹', '3p', '3P', '腰', '臀', '肉棒', '啪啪', '圣水']
  601. var politic_list = ['中共', '土共', '民主', '法治', '天安门', '老兵', '自由', '殴打', '近平', '奴隶', '道德', '强拆', '法律', '监狱', '城管', '土匪', '毛腊肉', '老毛', '专制', '社会主义', '党中央', '统治', '酷刑', '党员', '人民', '民族', 'P2P', '普京', '英雄', '纳税', '维权', '执法', '警察', '独裁', '政府', '郭文贵', '文革', '武装', '共产', '垬', '毛泽东', '政权', '枪', '举报', '恐怖', '宗教', '卖国', '死', '软禁']
  602. var key_word_list = porn_list.concat(politic_list)
  603. var porn_detection = key_word_list.some(function(ele) {
  604. return title.indexOf(ele) > -1
  605. })
  606. porn_detection && ele.parentNode.removeChild(ele)
  607. // ele.parentNode.removeChild(ele);
  608. }
  609. });
  610. }
  611. }
  612. },
  613. {
  614. rule: "default",
  615. hostname: "jandan.net",
  616. regexp: "",
  617. action: () => {
  618. // html 调色
  619. let _html = _$('html', 'return_one')
  620. let _body = _$('body', 'return_one')
  621. _html.style.backgroundColor = '#333'
  622. _body.style.backgroundColor = '#333'
  623. // 去掉某些ID的发图
  624. let title_list = document.querySelectorAll("[title^=防伪码]")
  625. let block_user_list = ["42c968079f1cc3495692a053f432e105142a3142"]
  626. Array.prototype.forEach.call(title_list, (titleDOM) => {
  627. let author_id = titleDOM.title.replace(/[\u4e00-\u9fa5]{3}:/, '') // 装B正则
  628. if (block_user_list.includes(author_id)) {
  629. let remove_div = titleDOM.parentNode.parentNode.parentNode.parentNode
  630. remove_div.parentNode.removeChild(remove_div)
  631. }
  632. })
  633. }
  634. },
  635. {
  636. rule: "default",
  637. hostname: "www.sogou.com",
  638. regexp: "",
  639. action: () => {
  640. // html 调色
  641. console.log('sdasdasda')
  642. let id;
  643. id = setInterval(() => {
  644. let right = _$("#right", "return_one");
  645. if (right) {
  646. _$("#right", "remove_one");
  647. clearInterval(id)
  648. }
  649. }, 300);
  650. }
  651. },
  652. {
  653. rule: "default",
  654. hostname: "10.0.74.227",
  655. regexp: "",
  656. action: () => {
  657. setTimeout( _ => {
  658. let title_list = document.querySelectorAll(".opblock-tag")
  659. Array.prototype.forEach.call(title_list, (titleDOM) => {
  660. titleDOM.click()
  661. })
  662. }, 10 * 1000)
  663. }
  664. },
  665. ]
  666.  
  667.  
  668. let matched_rule = rules.filter( rule_item => rule_item.hostname instanceof Array
  669. ? rule_item.hostname.includes(hostname)
  670. : rule_item.hostname === hostname
  671. )[0]
  672.  
  673. return matched_rule
  674. }
  675.  
  676. //window.frames
  677. //Array.prototype.slice.call(ele)
  678.  
  679. function sleep(ms = 1500) {
  680. return new Promise(resolve => setTimeout(_ => resolve(), ~~ms))
  681. }
  682.  
  683. //寻找页面的主要内容
  684. function main_ele_searcher() {
  685. let article = document.getElementsByTagName('article')
  686. let main = document.getElementsByTagName('main')
  687. let content = (article.length && article[0]) || (main.length && main[0]) || false
  688. if (content) {
  689. return {
  690. main_ele: content,
  691. body_width: content.style.width,
  692. body_height: content.style.height
  693. };
  694. }
  695.  
  696. var ele = document.querySelectorAll("*");
  697. var arr = [];
  698. //var arr_index = [];
  699. var w, h;
  700.  
  701. var body_width = document.body.clientWidth;
  702. var body_height = document.body.clientHeight;
  703.  
  704. if (!body_height) {
  705. var max_width = 0, max_height = 0;
  706. document.documentElement.style.height = '100%';
  707. document.body.style.height = '100%';
  708.  
  709. for (let i = 0, ele_length = ele.length; i < ele_length; i++) {
  710. w = ele[i].clientWidth;
  711. h = ele[i].clientHeight;
  712. if (w > max_width) { max_width = w; }
  713. if (h > max_height) { max_height = h; }
  714. w = null;
  715. h = null;
  716. }
  717. body_width = max_width;
  718. body_height = max_height;
  719. }
  720.  
  721.  
  722. for (let i = 0, ele_length = ele.length; i < ele_length; i++) {
  723. w = ele[i].clientWidth;
  724. h = ele[i].clientHeight;
  725. //console.log(h);
  726.  
  727. if (w && h && w > body_width / 2 && h > body_height / 5 && w < body_width && h < body_height) {
  728. arr[w * h] = ele[i];
  729. //console.log(ele[i]);
  730. //arr_index.push(w * h);
  731. }
  732. w = null;
  733. h = null;
  734. }
  735. var main_ele = arr[arr.length - 1] || document.body;
  736. return {
  737. main_ele,
  738. body_width,
  739. body_height
  740. };
  741. }
  742.  
  743.  
  744. function content_purge() {
  745. let article = document.getElementsByTagName('article')
  746. let main = document.getElementsByTagName('main')
  747. let content = (article.length && article[0]) || (main.length && main[0]) || false;
  748. if (content) {
  749. main.style.cssText = 'width: 900px;margin: 40px 0 0 120px;float: left; ';
  750. document.body.innerHTML = '';
  751. document.body.appendChild(main);
  752. }
  753.  
  754. // main.parentNode.removeChild(main)
  755. // Array.from(document.body.children)
  756. // document.body.insertBefore(main, document.body.firstChild);
  757.  
  758. }
  759.  
  760. //去除侧边栏这样“狭长”的内容
  761. function ele_remover(obj) {
  762. var w, h;
  763. var { main_ele, body_width, body_height } = obj;
  764. var ele = document.querySelectorAll("*");
  765. //console.log(main_ele,body_width, body_height )
  766. for (let i = 0, ele_length = ele.length; i < ele_length; i++) {
  767. w = ele[i].clientWidth;
  768. h = ele[i].clientHeight;
  769. // main_ele.parentNode.children
  770. if (!ele[i].contains(main_ele) && !main_ele.contains(ele[i]) && (ele[i] !== main_ele) && w * 1.4 < h && w < body_width / 2 && w > body_width / 10) {
  771. //content_adjust(ele[i]);
  772. console.log("---------element removed ------------");
  773. console.log(ele[i]);
  774. console.log("---------element removed ------------");
  775.  
  776. ele[i].parentNode.removeChild(ele[i]);
  777. }
  778. w = null;
  779. h = null;
  780. }
  781. }
  782.  
  783. //去除被移除元素的兄弟元素的padding margin
  784. function content_adjust(ele) {
  785. var siblings = ele.parentNode.children;
  786. ele.parentNode.style.position = "relative";
  787. // console.log(ele);
  788. for (let i = 0, ele_length = siblings.length; i < ele_length; i++) {
  789. //console.log(siblings[i]);
  790. siblings[i].setAttribute('style', 'position:relative;padding:20px;margin:0px;overflow:visible;');
  791. }
  792. }
  793.  
  794.  
  795. //调整主要内容的样式
  796. function main_content_adjust(main_ele) {
  797. //var a = [{x,y,top,right,bottom,left,width}]=main_ele.getClientRects();
  798. main_ele.parentNode.style.position = "relative";
  799. main_ele.style.cssText = "position:absolute; top:0;right:0; bottom:0;left:0;"
  800. // main_ele.setAttribute('style','position:relative;padding:20px 20px 20px 40px;margin:auto;clear:both;overflow:visible;');
  801. var ratio = 0.9 * (window.innerWidth) / main_ele.clientWidth;
  802. var ele = main_ele.querySelectorAll('*');
  803. // console.log(ele);
  804. for (let i = 0, ele_length = ele.length; i < ele_length; i++) {
  805. ele[i].style.width = ele[i].clientWidth * ratio + "px";
  806. ele[i].style.overflow = 'visible';
  807. //console.log(i);
  808. }
  809.  
  810. }
  811.  
  812.  
  813.  
  814. // body 全屏
  815. function full_body() {
  816. let root = document.body
  817. root.style.cssText = "position:absolute; top:0;right:0; bottom:0;left:0;"
  818.  
  819. }
  820. //背景调色
  821. function change_style(DOM_node = document.body) {
  822. return
  823. // 执行速度 : for 循环 > forEach > 尾递归 >递归 (迭代)
  824. let background_base_color = 50;
  825. let background_offset = 20;
  826. let font_base_color = 230;
  827. let font_offset = 20;
  828. var all = (DOM_node || document).querySelectorAll('*');
  829. for (let i = 0, ele_length = all.length; i < ele_length; i++) {
  830. let item = all[i]
  831. let item_style = item.style
  832. let tag_exclude_check = !('script,style,img').includes(item.nodeName.toLowerCase())
  833. if(tag_exclude_check && !item_style.backgroundColor){
  834. item_style.backgroundColor = random_color(background_base_color,background_offset);
  835. item_style.color = random_color(font_base_color,font_offset);
  836. }
  837. }
  838. /*
  839. let background_base_color = 40;
  840. let background_offset = 20;
  841. let font_base_color = 200;
  842. let font_offset = 50;
  843. let html_str = DOM_node.innerHTML ;
  844. // 匹配速度 string.indexOf > string.match
  845. // let html_str = DOM_node.innerHTML.replace(/\&/g,'') ;
  846. // var all = (new DOMParser()).parseFromString(html_str, "text/xml");
  847. let html_str_arr = html_str.split(">");
  848. let new_html_str_arr = html_str_arr.map((item, index) => {
  849. if(item.indexOf('</') > 0) {return item }
  850. if(!item.match(/\<div|\<form|\<table|\<dl|\<ol|\<ul|\<pre|\<h|\<a|\<p|\<code|\<em|\<span|\<td|\<th|\<input|\<textarea/i)) {return item }
  851. let font_color_str = ";color:"+random_color(font_base_color,font_offset);
  852. if(item.indexOf('background-color') > 0) {
  853. let reg = /background-color\s*\:\s*[\w\,\(\)\#]+/;
  854. let str = 'background-color:' + random_color(background_base_color,background_offset) + font_color_str;
  855. return item.replace(reg, str)
  856. }
  857. if(item.indexOf('background') > 0) {
  858. let reg = /\#[^\s]+|rgb[^\)]+\)/;
  859. let str = random_color(background_base_color,background_offset);
  860. return item.replace(reg, str).replace(/(style=(["'])[^'"]+)\2/,"$1" + font_color_str + "$2")
  861. }
  862. return `${item} style="background-color:${random_color(background_base_color,background_offset)}${font_color_str}"`;
  863. })
  864.  
  865. DOM_node.innerHTML = new_html_str_arr.join(">")
  866. DOM_node.style.backgroundColor = random_color(background_base_color,background_offset);
  867. */
  868. }
  869.  
  870. function random_color(base_color, offset, ratio=1){
  871. base_color = (base_color & 255) === (~~base_color) ? ~~base_color : 255;
  872. offset = ~~offset
  873. offset = (base_color+offset > 0 && base_color+offset < 256)? offset : 0
  874. return `rgba(${(base_color + offset*Math.random())&255},${(base_color + offset*Math.random())&255},${(base_color + offset*Math.random())&255},0.8)`;
  875. }
  876.  
  877.  
  878. function ele_hide(e) {
  879. // if (document.body.hasChildNodes(e)) e.style.display = "none";
  880. document.body.hasChildNodes(e) && e.length !== 0 && e.style.setAttribute('display', "none");
  881. }
  882. function ele_hide_all(all) {
  883. Array.prototype.forEach.call(all, function (e) { e.style.display = "none"; });
  884. }
  885. function ele_remove(e) {
  886. // if (document.body.hasChildNodes(e)) e.parentNode.removeChild(e);
  887. e = e.length ? e[0] : e
  888. document.body.hasChildNodes(e) && e.length !== 0 && e.parentNode.removeChild(e);
  889. }
  890. function ele_remove_all(all) {
  891. if (all) {
  892. all = Array.from(all)
  893. }
  894. if (!all.length) {
  895. return
  896. }
  897. Array.prototype.forEach.call(all, function (e) {
  898. e instanceof Object && e.parentNode.removeChild(e);
  899. });
  900. }
  901.  
  902.  
  903. function _$(selector, operation_code) {
  904. // operation_code : return_one return_all remove_one remove_all hide_one hide_all
  905. function get(selector) {
  906. if((/^#[^.#]+$/).test(selector)) {
  907. return [document.getElementById(selector.slice(1, selector.length))]
  908. }
  909. if((/^\.[^.#]+$/).test(selector)) {
  910. return document.getElementsByClassName(selector.slice(1, selector.length))
  911. }
  912. if((/^[^.#\[\]=]+$/).test(selector)) {
  913. return document.getElementsByTagName(selector)
  914. }
  915. return document.querySelectorAll(selector)
  916. }
  917. switch (operation_code) {
  918. case 'return_one': //return_one
  919. // return document.querySelector(selector);
  920. return get(selector)[0];
  921. case 'return_all':// return_all
  922. // return document.querySelectorAll(selector);
  923. return get(selector);
  924. case 'remove_one': //remove_one
  925. // ele_remove(document.querySelector(selector));
  926. ele_remove(get(selector));
  927. break;
  928. case 'remove_all': //remove_all
  929. // ele_remove_all(document.querySelectorAll(selector));
  930. ele_remove_all(get(selector));
  931. break;
  932. case 'hide_one': //hide_one
  933. // ele_hide(document.querySelector(selector));
  934. ele_hide(get(selector));
  935. break;
  936. case 'hide_all':// hide_all
  937. // ele_hide_all(document.querySelectorAll(selector));
  938. ele_hide_all(get(selector));
  939. break;
  940.  
  941. default:
  942. // var result = document.querySelectorAll(selector);
  943. var result = get(selector);
  944. return result.length > 1 ? result : result[0];
  945. }
  946. }
  947.  
  948. function _CSS(DOM_ele, pseudo_class) {
  949. return pseudo_class ? window.getComputedStyle(DOM_ele, pseudo_class) : window.getComputedStyle(DOM_ele)
  950. }
  951.  
  952. function revert_color(color) {
  953. const [red, blue, green, alpha] = color.match(/\d{1,3}/g);
  954. return Number(alpha) ? `rgba(${255 - red}, ${255 - blue}, ${255 - green}, ${alpha})` : `rgb(${255 - red}, ${255 - blue}, ${255 - green})`;
  955. }
  956.  
  957. function remove_div_padding_margin(ele) {
  958. if (ele) {
  959. ele.setAttribute('style', 'overflow:visible;position:relative;padding:0px;margin:0px;left:0px;');
  960. }
  961. if (ele && ele.children.length > 0) {
  962. Array.prototype.forEach.call(ele.children, (function (e) {
  963. remove_div_padding_margin(e);
  964. }));
  965. }
  966. }
  967.  
  968.  
  969. function getFormatDate(date) {
  970. let year = new Date(date).getFullYear()
  971. let month = new Date(date).getMonth() + 1
  972. let day = new Date(date).getDate()
  973. month = month < 10 ? '0' + month : month
  974. day = day < 10 ? '0' + day : day
  975. return [year, month, day].join('-')
  976. }
  977.  
  978. function postData(url, data) {
  979. const opts = {
  980. method: 'POST', // 请求方法
  981. body: JSON.stringify(data), // 请求体
  982. headers: {
  983. accept: 'application/json',
  984. 'content-type': 'application/json'
  985. },
  986. mode: 'cors'
  987. }
  988. return fetch(url, opts)
  989. .then(response => {
  990. return response.json()
  991. })
  992. .then(resp => {
  993. return resp
  994. // return Promise.reject(resp)
  995. })
  996. }
  997.  
  998. function content_get() {
  999. var ajax = new XMLHttpRequest();
  1000. ajax.open('get', '/');
  1001. ajax.send();
  1002. ajax.onreadystatechange = function () {
  1003. if (ajax.readyState == 4 && ajax.status == 200) {
  1004. content_new = ajax.responseText;
  1005. //console.log(content_new);
  1006. content_old = content_old || content_new;
  1007. if (content_new !== content_old) {
  1008. location.reload();
  1009. }
  1010. }
  1011. };
  1012.  
  1013. setTimeout(content_get, 1000);
  1014. }