百度贴吧图片缩放增强脚本

增强百度贴吧图片缩放,看大图无需开新标签页。

  1. // ==UserScript==
  2. // @id tieba.baidu.com-709c0fe7-e313-44bd-9dbd-752bbd80259d@patwonder@163.com
  3. // @name 百度贴吧图片缩放增强脚本
  4. // @version 0.78
  5. // @namespace patwonder@163.com
  6. // @author patwonder
  7. // @description 增强百度贴吧图片缩放,看大图无需开新标签页。
  8. // @include /^https?://tieba\.baidu\.com/((f\?kz=.*)|(p/.*))/
  9. // @include http://tieba.baidu.com/club/*/p/*
  10. // @include http://tieba.baidu.com/f?kz=*
  11. // @include http://tieba.baidu.com/p/*
  12. // @include http://tieba.baidu.com/f?*ct=*z=*
  13. // @include http://tieba.baidu.com.cn/f?kz=*
  14. // @include http://tieba.baidu.com.cn/p/*
  15. // @include http://tieba.baidu.com.cn/f?*ct=*z=*
  16. // @include http://post.baidu.com/f?kz=*
  17. // @include http://post.baidu.com/p/*
  18. // @include http://post.baidu.com/f?*ct=*z=*
  19. // @include http://post.baidu.com.cn/f?kz=*
  20. // @include http://post.baidu.com.cn/p/*
  21. // @include http://post.baidu.com.cn/f?*ct=*z=*
  22. // @run-at document-end
  23. // @grant none
  24. // ==/UserScript==
  25.  
  26. (function(d, w) {
  27. var B_WIDESCREEN_ENABLED = true;
  28. var B_FLOORNUM_ENABLED = true;
  29. var B_SWITCH_ENABLED = true;
  30.  
  31. var btise = { wideScreenEnabled : B_WIDESCREEN_ENABLED, floorNumEnabled : B_FLOORNUM_ENABLED };
  32. var localStorage = null;
  33. try {
  34. localStorage = w.localStorage;
  35. } catch (ex) { }
  36.  
  37. if (localStorage) {
  38. localStorage = localStorage.wrappedJSObject || localStorage;
  39. if (typeof(localStorage.btise) == 'string') {
  40. try {
  41. btise = JSON.parse(localStorage.btise);
  42. } catch (ex) { }
  43. }
  44.  
  45. // Assign default configs
  46. if (typeof(btise.wideScreenEnabled) == 'undefined' && typeof(btise.floorNumEnabled) == 'undefined') {
  47. btise.wideScreenEnabled = B_WIDESCREEN_ENABLED;
  48. btise.floorNumEnabled = B_FLOORNUM_ENABLED;
  49. }
  50. if (typeof(localStorage.btise) != 'string')
  51. localStorage.btise = JSON.stringify(btise);
  52. } else {
  53. B_SWITCH_ENABLED = false;
  54. }
  55.  
  56. B_WIDESCREEN_ENABLED = !!btise.wideScreenEnabled;
  57. B_FLOORNUM_ENABLED = !!btise.floorNumEnabled;
  58.  
  59. var STR_SCRIPT_NAME = '百度贴吧图片缩放增强脚本';
  60.  
  61. var common = {
  62. matchesSelector: function(element, selector) {
  63. if (element.mozMatchesSelector) {
  64. return element.mozMatchesSelector(selector);
  65. } else if (element.webkitMatchesSelector) {
  66. return element.webkitMatchesSelector(selector);
  67. } else if (element.matchesSelector) {
  68. return element.matchesSelector(selector);
  69. } else {
  70. try {
  71. var elems = element.parentElement ? element.parentElement.querySelectorAll(selector) : [];
  72. for (var i = 0, l = elems.length; i < l; i++) {
  73. if (elems[i] === element) return true;
  74. }
  75. } catch (ex) { }
  76. return false;
  77. }
  78. },
  79. // re-entrance guard for image wrapping, avoid re-entering the DOM mutation event handlers
  80. wrapping: false
  81. };
  82.  
  83. (function() {
  84. var IMG_RETRIEVING_SIZE = '正在获取原始大小……';
  85. var IMG_ORG_SIZE_DESC = '原图:';
  86. var IMG_CUR_PERCENT = "比例:";
  87. var IMG_MAG_DESC = '点击放大,按住Shift全部放大';
  88. var IMG_MIN_DESC = '点击缩小,按住Shift全部缩小';
  89. var SIGN_SELECTOR = 'div.d_sign_split + img';
  90. var IMG_INSIDE_EDITOR_SELECTOR = '#editor img, #tb_rich_poster img';
  91. var IMG_SELECTOR = 'img.BDE_Image, div.p_content img.BDE_Smiley, img.d_content_img, ' + SIGN_SELECTOR;
  92. var REG_SIGN = /w%3D580.*\/sign=.*?(?=\/)/;
  93. var REG_TBPICAU = /\/(\w+)\.[a-zA-Z]{3,4}\?.*tbpicau=[\w-_]+/;
  94. var REG_TID = /(?:\/p\/|[\?&]kz=)(\d+)/;
  95. var images = [];
  96.  
  97. var matchesSelector = common.matchesSelector;
  98.  
  99. var prefilterImages = function() {
  100. var imageNodes = d.querySelectorAll(IMG_SELECTOR);
  101. for (var i = 0; i < imageNodes.length; i++) {
  102. var image = imageNodes[i];
  103. if (shouldAdd(image))
  104. prefilterImage(image);
  105. }
  106. };
  107.  
  108. var prefilterImage = function(image) {
  109. // Check whether we should reload with original src
  110. if (REG_SIGN.test(image.src)) {
  111. if (REG_TBPICAU.test(image.src)) {
  112. checkTbpicau(image, image.src);
  113. } else {
  114. var newimg = d.createElement('img');
  115. newimg.src = image.src.replace(REG_SIGN, "pic/item");
  116. newimg.className = image.className;
  117. if (image.parentElement) {
  118. common.wrapping = true;
  119. image.parentElement.insertBefore(newimg, image);
  120. image.parentElement.removeChild(image);
  121. common.wrapping = false;
  122. return newimg;
  123. }
  124. }
  125. return image;
  126. }
  127.  
  128. // Check for passively loaded images
  129. var passiveAttr;
  130. var passiveSrc;
  131. if (image.hasAttribute("data-passive")) {
  132. passiveAttr = "data-passive";
  133. } else if (image.hasAttribute("data-tb-lazyload")) {
  134. passiveAttr = "data-tb-lazyload";
  135. }
  136.  
  137. if (passiveAttr) {
  138. passiveSrc = image.getAttribute(passiveAttr);
  139. if (REG_TBPICAU.test(passiveSrc)) {
  140. checkTbpicau(image, passiveSrc);
  141. } else {
  142. image.setAttribute(passiveAttr, passiveSrc.replace(REG_SIGN, "pic/item"));
  143. }
  144. }
  145.  
  146. return image;
  147. };
  148.  
  149. var tidRes = REG_TID.exec(w.location.href);
  150. var tid = tidRes && tidRes[1];
  151. var checkTbpicau = function(image, src) {
  152. var tbpicauRes = REG_TBPICAU.exec(src);
  153. if (tbpicauRes && tid) {
  154. var xhttp = new XMLHttpRequest();
  155. xhttp.onreadystatechange = function() {
  156. if (this.readyState == 4 && this.status == 200) {
  157. var waterurl = null;
  158. try {
  159. waterurl = JSON.parse(this.responseText).data.img.original.waterurl;
  160. } catch(ex) {}
  161. if (waterurl) {
  162. var passiveSrc = image.getAttribute('data-passive') || image.getAttribute('data-tb-lazyload');
  163. if (passiveSrc && passiveSrc != "loaded") {
  164. if (image.hasAttribute("data-passive")) {
  165. image.setAttribute("data-passive", waterurl);
  166. } else {
  167. image.setAttribute("data-tb-lazyload", waterurl);
  168. }
  169. } else {
  170. var newimg = d.createElement('img');
  171. newimg.src = waterurl;
  172. newimg.className = image.className;
  173. if (image.parentElement) {
  174. common.wrapping = true;
  175. image.parentElement.insertBefore(newimg, image);
  176. image.parentElement.removeChild(image);
  177. common.wrapping = false;
  178. replacePrefilteredImage(image, newimg);
  179. }
  180. }
  181. }
  182. }
  183. };
  184. var picId = tbpicauRes[1];
  185. xhttp.open("GET", "https://tieba.baidu.com/photo/p?alt=jview&pic_id=" + picId + "&tid=" + tid, true);
  186. xhttp.send();
  187. }
  188. };
  189.  
  190. var obtainImages = function() {
  191. var imageNodes = d.querySelectorAll(IMG_SELECTOR);
  192. for (var i = 0; i < imageNodes.length; i++) {
  193. var image = imageNodes[i];
  194. if (shouldAdd(image))
  195. images.push(image);
  196. }
  197. };
  198. var shouldAdd = function(image) {
  199. // don't process images inside the editor
  200. if (matchesSelector(image, IMG_INSIDE_EDITOR_SELECTOR))
  201. return false;
  202. return true;
  203. };
  204. var addImages = function(new_images) {
  205. for (var i = 0; i < new_images.length; i++) {
  206. var image = new_images[i];
  207. if (!shouldAdd(image)) continue;
  208. image = prefilterImage(image);
  209.  
  210. images.push(image);
  211. initImage(image);
  212. adjustScaling(image);
  213. }
  214. };
  215. var removeImages = function(del_images) {
  216. for (var i = 0; i < del_images.length; i++) {
  217. var image = del_images[i];
  218. for (var j = 0; j < images.length; j++) {
  219. if (images[j] == image) {
  220. images.splice(j, 1);
  221. break;
  222. }
  223. }
  224. }
  225. };
  226. var replacePrefilteredImage = function(oldImage, newImage) {
  227. for (var j = 0; j < images.length; j++) {
  228. if (images[j] == oldImage) {
  229. images.splice(j, 1);
  230. break;
  231. }
  232. }
  233.  
  234. images.push(newImage);
  235. initImage(newImage);
  236. adjustScaling(newImage);
  237. };
  238. w.addEventListener('DOMNodeInserted', function(event) {
  239. if (common.wrapping) return;
  240. if (matchesSelector(event.target, IMG_SELECTOR)) {
  241. addImages([event.target]);
  242. } else if (event.target.querySelectorAll) {
  243. var new_images = event.target.querySelectorAll(IMG_SELECTOR);
  244. if (new_images.length !== 0) {
  245. addImages(new_images);
  246. }
  247. }
  248. }, false);
  249. w.addEventListener('DOMNodeRemoved', function(event) {
  250. if (common.wrapping) return;
  251. if (matchesSelector(event.target, IMG_SELECTOR)) {
  252. removeImages([event.target]);
  253. } else if (event.target.querySelectorAll) {
  254. var del_images = event.target.querySelectorAll(IMG_SELECTOR);
  255. if (del_images.length !== 0) {
  256. removeImages(del_images);
  257. }
  258. }
  259. }, false);
  260. // Stops everything from bubbling up and doing anything else
  261. var stopListener = function(e) {
  262. if (e.stopImmediatePropagation) e.stopImmediatePropagation();
  263. else if (e.stopPropagation) e.stopPropagation();
  264.  
  265. if (e.preventDefault) e.preventDefault();
  266.  
  267. return false;
  268. };
  269. // Image click handler that handles image size switching
  270. var listener = function(e) {
  271. // Only handle left clicks
  272. if (e.button !== 0) return;
  273.  
  274. var image = (e && e.target) || (w.event && w.event.srcElement);
  275. // We are expecting clicks on detected images
  276. if (!image || image.localName != 'img' || image.getAttribute('data-detected') != 'true' || !shouldAdd(image))
  277. return;
  278.  
  279. if (image && image.getAttribute('data-disabled') != 'true') {
  280. var isSign, img, i;
  281. if (image.getAttribute('data-fullsized') == 'true') {
  282. image.setAttribute('data-fullsized', 'false');
  283. setTitle(image);
  284. if (e.shiftKey) {
  285. isSign = matchesSelector(image, SIGN_SELECTOR);
  286. for (i = 0; i < images.length; i++) {
  287. img = images[i];
  288. // separate scale all images for sign and non-sign images
  289. if (isSign != matchesSelector(img, SIGN_SELECTOR))
  290. continue;
  291. if (img.getAttribute('data-fullsized') == 'true') {
  292. img.setAttribute('data-fullsized', 'false');
  293. setTitle(img);
  294. }
  295. }
  296. }
  297. } else {
  298. image.setAttribute('data-fullsized', 'true');
  299. setTitle(image);
  300. if (e.shiftKey) {
  301. isSign = matchesSelector(image, SIGN_SELECTOR);
  302. for (i = 0; i < images.length; i++) {
  303. img = images[i];
  304. // separate scale all images for sign and non-sign images
  305. if (isSign != matchesSelector(img, SIGN_SELECTOR))
  306. continue;
  307. if (img.getAttribute('data-fullsized') != 'true') {
  308. img.setAttribute('data-fullsized', 'true');
  309. setTitle(img);
  310. }
  311. }
  312. }
  313. }
  314. if (e.shiftKey) image.scrollIntoView();
  315. }
  316. return stopListener(e);
  317. };
  318.  
  319. var isDisplayingFullsize = function(image, callback) {
  320. if (image.hasAttribute('data-owidth')) {
  321. var owidth = image.getAttribute('data-owidth');
  322. callback(image.parentElement.offsetWidth >= owidth);
  323. return;
  324. }
  325.  
  326. var newImg = new Image();
  327.  
  328. newImg.onload = function() {
  329. var owidth = newImg.width;
  330. image.setAttribute('data-owidth', owidth);
  331. image.setAttribute('data-oheight', newImg.height);
  332. // image may have detached from the document, thus visiting "parentElement" may fail
  333. if (image.parentElement && image.parentElement.offsetWidth)
  334. callback(image.parentElement.offsetWidth >= owidth);
  335. };
  336.  
  337. var passiveSrc = image.getAttribute('data-passive') || image.getAttribute('data-tb-lazyload');
  338. newImg.src = (passiveSrc != "loaded" && passiveSrc) || image.src;
  339. };
  340.  
  341. var adjustScaling = function(image) {
  342. isDisplayingFullsize(image, function(isFullsize) {
  343. if (isFullsize) {
  344. if (image.getAttribute('data-disabled') != 'true') {
  345. image.setAttribute('data-disabled', 'true');
  346. }
  347. } else {
  348. if (image.getAttribute('data-disabled') == 'true') {
  349. image.removeAttribute('data-disabled');
  350. }
  351. }
  352. setTitle(image);
  353. });
  354. };
  355.  
  356. var initImage = function(image) {
  357. image.removeAttribute('width');
  358. image.removeAttribute('height');
  359. image.setAttribute('data-detected', 'true');
  360. setTitle(image);
  361. image.onclick = undefined;
  362.  
  363. image.addEventListener("load", function() {
  364. var passiveSrc = image.getAttribute('data-passive') || image.getAttribute('data-tb-lazyload');
  365. if (passiveSrc && passiveSrc != "loaded")
  366. return;
  367. var width = image.naturalWidth;
  368. var height = image.naturalHeight;
  369. image.setAttribute('data-owidth', width);
  370. image.setAttribute('data-oheight', height);
  371. adjustScaling(image);
  372. }, false);
  373.  
  374. if (matchesSelector(image, SIGN_SELECTOR)) {
  375. // wrap sign images into a fixed size container
  376. common.wrapping = true;
  377. try {
  378. var divWrapper = d.createElement('div');
  379. divWrapper.className = 'd_sign_wrapper';
  380. image.parentElement.insertBefore(divWrapper, image.previousSibling);
  381. divWrapper.appendChild(image.previousSibling);
  382. divWrapper.appendChild(image);
  383. } finally {
  384. common.wrapping = false;
  385. }
  386. }
  387. };
  388.  
  389. var setTitle = function(image) {
  390. var title_a = [];
  391. if (image.getAttribute('data-disabled') != 'true') {
  392. var isFullsized = image.getAttribute('data-fullsized') == 'true';
  393. if (image.hasAttribute('data-owidth')) {
  394. var owidth = image.getAttribute('data-owidth');
  395. title_a = [IMG_ORG_SIZE_DESC, owidth, '*', image.getAttribute('data-oheight'), ' ',
  396. IMG_CUR_PERCENT, isFullsized ? 100 : Math.floor(100 * image.parentElement.offsetWidth / owidth), '%\n'];
  397. } else {
  398. title_a = [IMG_RETRIEVING_SIZE, '\n'];
  399. }
  400. title_a.push(isFullsized ? IMG_MIN_DESC : IMG_MAG_DESC);
  401. }
  402. image.setAttribute('title', title_a.join(''));
  403. };
  404.  
  405. var doImageOpAll = function(op) {
  406. for (var i = 0; i < images.length; i++) {
  407. op(images[i]);
  408. }
  409. };
  410.  
  411. var adjustScalingAll = function() { doImageOpAll(adjustScaling); };
  412. var initImageAll = function() { doImageOpAll(initImage); };
  413.  
  414. prefilterImages();
  415. obtainImages();
  416. initImageAll();
  417. w.addEventListener('resize', adjustScalingAll, false);
  418.  
  419. var loadListener = function() {
  420. for (var i = 0; i < images.length; i++) {
  421. var image = images[i];
  422. adjustScaling(image);
  423. image.onclick = undefined;
  424. }
  425. };
  426. w.addEventListener('DOMContentLoaded', loadListener, false);
  427. w.addEventListener('load', loadListener, false);
  428. w.addEventListener('click', listener, true);
  429.  
  430. var rightPanelWidth = (function(rightSection) {
  431. return rightSection ? rightSection.offsetWidth : 0;
  432. })(d.querySelector('div.right_section'));
  433.  
  434. var style = d.createElement('style');
  435. style.setAttribute('type','text/css');
  436.  
  437. var aInnerHTML = ['img[data-detected=true] { max-width: 100% !important; margin-top: 0 !important; width: auto !important; height: auto !important; cursor: url("data:image/gif;base64,R0lGODlhIAAgAKEAAP///wAAAP///////yH5BAEAAAIALAAAAAAgACAAAAJMlBUZx+2PApggwesk3Qt7XYGdB4EhR5aToqzpo7GJ+zbmTI31IYp7tkH9bDfFMGOM6I4wC/OSfDaXUl71is1qt9yu9wsOi8fkstlQAAA7") 6 6, pointer !important; }\n',
  438. 'img[data-detected=true][data-fullsized=true]:not([data-disabled=true]) { max-width: none !important; cursor: url("data:image/gif;base64,R0lGODlhIAAgAKEAAP///wAAAP///////yH5BAEAAAIALAAAAAAgACAAAAJLlBUZx+2PApggwesk3Qt7vU2dB4GhSJaikqBptrLuy5jnSB82hefGPvPpTCxhRvGzGDHI5aXpfECjjR71is1qt9yu9wsOi8fk8rgAADs=") 6 6, pointer !important; z-index: 9999999; position: relative; }\n',
  439. 'img[data-detected=true][data-disabled=true] { cursor: default !important; }\n',
  440. 'div.replace_div { width: auto !important; height: auto !important; overflow: visible !important; position: static !important; border: none !important; }\n',
  441. 'div.replace_tip { display: none !important; }\n',
  442. 'div.d_sign_wrapper { max-width: 568px; margin: 0 5px; }\n',
  443. 'div.d_sign_split, div.d_sign_split + img { margin-left: 0 !important; margin-right: 0 !important; }\n',
  444. '#pic_to_album_tip { display: none !important; }\n',
  445. 'div.d_post_content_main, div.d_post_content { overflow: visible !important; }\n',
  446. 'div.j_lzl_container img.BDE_Smiley { max-width: 30px !important; max-height: 30px !important; }\n',
  447. 'ul.nav_right a { height: 36px; float: right; color: #5C6573; margin: 5px 5px; padding: 10px 5px; }\n',
  448. 'div.p_content.p_content_nameplate > img[width="1"][height="1"] { display: none !important; }',
  449. 'body { overflow-x: auto !important; }'];
  450. if (B_WIDESCREEN_ENABLED) {
  451. aInnerHTML = aInnerHTML.concat(['\n',
  452. '#container, div.content, #tb_nav, div.p_thread, #pb_content, div.left_section, #j_core_title_wrap, div.l_post, div.d_post_content_main, div.core_reply_wrapper, div.pb_footer, div.d_sign_split, blockquote.d_quote, blockquote.d_quote fieldset, .d_quote .quote_content, div.core { width: auto !important; }\n',
  453. 'div.pb_content { background: none !important; }\n',
  454. 'div.d_post_content_main { padding: 15px 10px 5px !important; }\n',
  455. 'div.p_content { padding: 0 !important; }\n',
  456. 'div.left_section {\n',
  457. ' -moz-box-sizing: border-box;\n',
  458. ' -webkit-box-sizing: border-box;\n',
  459. ' box-sizing: border-box;\n',
  460. ' width: -moz-calc(100% - ', rightPanelWidth, 'px) !important;\n',
  461. ' width: -webkit-calc(100% - ', rightPanelWidth, 'px) !important;\n',
  462. ' width: calc(100% - ', rightPanelWidth, 'px) !important;\n',
  463. ' border-right: 1px solid #E5E5E5;\n',
  464. '}\n',
  465. 'div.l_post {\n',
  466. ' background-color: white;\n',
  467. ' background-image: -moz-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
  468. ' background-image: -webkit-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
  469. ' background-image: -ms-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
  470. ' background-image: -o-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
  471. ' background-image: linear-gradient(to right, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
  472. '}\n',
  473. 'div.core_reply_tail, div.core_reply_wrapper { margin-right: 0px !important; }\n',
  474. 'div.core_reply, div.d_content { margin-left: 0px !important; max-width: 568px; margin-right: auto !important; }\n',
  475. '#tb_rich_poster { margin-left: 0px !important; }\n',
  476. '#tb_rich_poster_container { margin-left: 10px !important; }\n',
  477. 'div.d_main_section, div.d_post_content_main, div.d_content, div.core_reply, h1.core_title_txt { float: none !important; }\n',
  478. 'div.d_post_content_main { margin-left: 130px !important; }\n',
  479. 'div.right_section { margin-left: 0 !important; }\n',
  480. 'h1.core_title_txt { display: inline; }\n',
  481. '.card_top_theme2 { margin-right: 0 !important; }\n',
  482. '.exp { width: 133px !important; }\n',
  483. '.exp_bar { margin-right: 0 !important; }\n',
  484. 'div.post_bubble_top, div.post_bubble_bottom { display: none !important; }\n',
  485. 'div.post_bubble_middle { width: auto !important; background: none !important; padding: 0 !important; }\n'
  486. ]);
  487. }
  488. style.innerHTML = aInnerHTML.join('');
  489. d.querySelector('head').appendChild(style);
  490.  
  491. adjustScalingAll();
  492. w.setTimeout(adjustScalingAll, 1000);
  493. })();
  494.  
  495. (function() {
  496. if (!B_FLOORNUM_ENABLED) return;
  497.  
  498. var POST_SELECTOR = 'div.l_post';
  499. var LZL_POST_SELECTOR = 'span.lzl_time';
  500. var GRAVE_POST_WARNING_THRESHOLD_MILLIS = 30 * 86400 * 1000;
  501.  
  502. var pint = function(str) {
  503. var num = parseInt(str, 10);
  504. return (isNaN(num) || !isFinite(num)) ? 0 : num;
  505. };
  506.  
  507. var getPostTimeMillis = function(postTimeString) {
  508. var postTimeMillis = 0;
  509. if (postTimeString) {
  510. var match = /^\s*([0-9]{2,4})[\/-]([0-9]{1,2})[\/-]([0-9]{1,2})[\stT\,]*([0-9]{1,2})\:([0-9]{1,2})(?:\:([0-9]{1,2}))?\s*$/.exec(postTimeString);
  511. if (match) {
  512. postTimeMillis = new Date(pint(match[1]), pint(match[2]) - 1, pint(match[3]), pint(match[4]), pint(match[5]), pint(match[6])).getTime();
  513. }
  514. }
  515. return postTimeMillis;
  516. };
  517.  
  518. var getPostTimeSummary = function(postTimeString) {
  519. var postTimeMillis = getPostTimeMillis(postTimeString);
  520. var postTimeSummary = '';
  521. if (postTimeMillis) {
  522. //alert(postTimeMillis + " ~ " + Date.now());
  523. var mins = Math.floor((Date.now() - postTimeMillis) / 60000);
  524. if (mins < 1)
  525. postTimeSummary = '刚刚';
  526. else if (mins < 60)
  527. postTimeSummary = mins + '分钟前';
  528. else {
  529. var hours = Math.floor(mins / 60);
  530. if (hours < 24)
  531. postTimeSummary = hours + '小时前';
  532. else {
  533. var days = Math.floor(hours / 24);
  534. if (days < 31)
  535. postTimeSummary = days + '天前';
  536. else {
  537. var months = Math.floor(days / 30.436875);
  538. if (months < 12)
  539. postTimeSummary = months + '个月前';
  540. else {
  541. var years = Math.floor(months / 12);
  542. months -= years * 12;
  543. postTimeSummary = years + '年' + ((months > 0 && years < 3) ? months + '个月' : '') + '前';
  544. }
  545. }
  546. }
  547. }
  548. postTimeSummary += ' ';
  549. }
  550. return postTimeSummary;
  551. };
  552.  
  553. var gravePostWarning = function(postTimeString) {
  554. var postTimeMillis = getPostTimeMillis(postTimeString);
  555. return ((Date.now() - postTimeMillis) >= GRAVE_POST_WARNING_THRESHOLD_MILLIS);
  556. };
  557.  
  558. var style = d.createElement('style');
  559. style.setAttribute('type','text/css');
  560. style.innerHTML = ['div.d_floor { font-family: sans-serif; font-size: 16px; float: right; margin: 5px; color: #261CDC; }\n',
  561. 'span.s_lzl_time_summary { color: rgb(153, 153, 153); }\n',
  562. 'div.louzhubiaoshi { right: auto !important; left: 0px; top: 0px !important; transform: rotate(-90deg); -moz-transform: rotate(-90deg); -webkit-transform: rotate(-90deg); }\n',
  563. 'div.louzhubiaoshi_wrap { position: static !important; }\n',
  564. 'div.l_post { position: relative; }\n',
  565. 'div.d_floor.grave_warning { color:white; background: red; }\n',
  566. 'div.d_post_content_main { background-color: transparent ! important }'
  567. ].join('');
  568. d.querySelector('head').appendChild(style);
  569.  
  570. var updatePost = function(post) {
  571. var field = null;
  572. try {
  573. field = JSON.parse(post.getAttribute('data-field'));
  574. } catch (ex) { }
  575. var floorNum = field && (field.content && (field.content.floor || field.content.post_no));
  576. if (floorNum && !isNaN(parseFloat(floorNum)) && isFinite(floorNum)) {
  577. var div = post.querySelector('div.d_floor') || d.createElement('div');
  578. var dateString = field.content.date;
  579. if (!dateString) {
  580. var spans = post.querySelectorAll('span.j_reply_data, .post-tail-wrap > .tail-info');
  581. var span = spans.length > 0 ? spans[spans.length - 1] : null;
  582. dateString = span ? span.textContent : '';
  583. }
  584. if (gravePostWarning(dateString))
  585. div.className = 'd_floor grave_warning';
  586. else
  587. div.className = 'd_floor';
  588. div.textContent = getPostTimeSummary(dateString) + '#' + floorNum;
  589. common.wrapping = true;
  590. post.insertBefore(div, post.querySelector('*'));
  591. common.wrapping = false;
  592. }
  593. };
  594. var updateLzlPost = function(lzlpost) {
  595. var postTimeSummary = getPostTimeSummary(lzlpost.textContent);
  596. var span = lzlpost.parentElement.querySelector('span.s_lzl_time_summary') || d.createElement('span');
  597. span.className = 's_lzl_time_summary';
  598. span.textContent = postTimeSummary;
  599. common.wrapping = true;
  600. lzlpost.parentElement.insertBefore(span, lzlpost);
  601. common.wrapping = false;
  602. };
  603. var updateFloorNumField = function() {
  604. var posts = d.querySelectorAll(POST_SELECTOR);
  605. var i;
  606. for (i = 0; i < posts.length; i++)
  607. updatePost(posts[i]);
  608.  
  609. var lzlposts = d.querySelectorAll(LZL_POST_SELECTOR);
  610. for (i = 0; i < lzlposts.length; i++)
  611. updateLzlPost(lzlposts[i]);
  612. };
  613.  
  614. updateFloorNumField();
  615. setInterval(updateFloorNumField, 30000);
  616.  
  617. // update time summary when elements are dynamically inserted
  618. var matchesSelector = common.matchesSelector;
  619. w.addEventListener('DOMNodeInserted', function(event) {
  620. if (common.wrapping) return;
  621. if (matchesSelector(event.target, LZL_POST_SELECTOR)) {
  622. updateLzlPost(event.target);
  623. } else {
  624. if (matchesSelector(event.target, POST_SELECTOR)) {
  625. updatePost(event.target);
  626. }
  627. if (event.target.querySelectorAll) {
  628. var posts = event.target.querySelectorAll(POST_SELECTOR);
  629. var i;
  630. for (i = 0; i < posts.length; i++)
  631. updatePost(posts[i]);
  632.  
  633. var lzlposts = event.target.querySelectorAll(LZL_POST_SELECTOR);
  634. for (i = 0; i < lzlposts.length; i++)
  635. updateLzlPost(lzlposts[i]);
  636. }
  637. }
  638. }, false);
  639.  
  640. })();
  641.  
  642. (function() {
  643. if (!B_SWITCH_ENABLED) return;
  644.  
  645. var prefs = [
  646. { name: '宽屏样式', id: 'wsenable', prefName: 'wideScreenEnabled', value: B_WIDESCREEN_ENABLED },
  647. { name: '楼层计数', id: 'fnenable', prefName: 'floorNumEnabled', value: B_FLOORNUM_ENABLED }
  648. ];
  649. var elemNavRight = d.querySelector('ul.nav_right');
  650. if (!elemNavRight) {
  651. var navbar = document.querySelector("#tb_nav");
  652. if (navbar) {
  653. elemNavRight = d.createElement("ul");
  654. elemNavRight.className = "nav_right";
  655. navbar.appendChild(elemNavRight);
  656. }
  657. }
  658. if (elemNavRight) {
  659. prefs.forEach(function(pref) {
  660. var strEnableDisable = pref.value ? '禁用' : '启用';
  661. var strText = strEnableDisable + pref.name;
  662. var liPrefItem = d.createElement('li');
  663. var aPrefItem = d.createElement('a');
  664. aPrefItem.setAttribute('id', pref.id);
  665. aPrefItem.setAttribute('title', strText + '\n(' + STR_SCRIPT_NAME + ')');
  666. aPrefItem.setAttribute('href', '#');
  667. aPrefItem.innerHTML = strText;
  668. aPrefItem.addEventListener('click', function() {
  669. btise[pref.prefName] = !pref.value;
  670. localStorage.btise = JSON.stringify(btise);
  671. w.location.reload();
  672. return false;
  673. });
  674. liPrefItem.appendChild(aPrefItem);
  675. elemNavRight.appendChild(liPrefItem);
  676. });
  677. }
  678. })();
  679.  
  680. })(document, window);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址