SE Preview on hover

Shows preview of the linked questions/answers on hover

当前为 2017-02-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SE Preview on hover
  3. // @description Shows preview of the linked questions/answers on hover
  4. // @version 0.2.2
  5. // @author wOxxOm
  6. // @namespace wOxxOm.scripts
  7. // @license MIT License
  8. // @match *://*.stackoverflow.com/*
  9. // @match *://*.superuser.com/*
  10. // @match *://*.serverfault.com/*
  11. // @match *://*.askubuntu.com/*
  12. // @match *://*.stackapps.com/*
  13. // @match *://*.mathoverflow.net/*
  14. // @match *://*.stackexchange.com/*
  15. // @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
  16. // @require https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js
  17. // @grant GM_addStyle
  18. // @grant GM_xmlhttpRequest
  19. // @connect stackoverflow.com
  20. // @connect superuser.com
  21. // @connect serverfault.com
  22. // @connect askubuntu.com
  23. // @connect stackapps.com
  24. // @connect mathoverflow.net
  25. // @connect stackexchange.com
  26. // @connect cdn.sstatic.net
  27. // @run-at document-end
  28. // @noframes
  29. // ==/UserScript==
  30.  
  31. /* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */
  32.  
  33. const PREVIEW_DELAY = 200;
  34. const CACHE_DURATION = 1 * 60 * 1000; // 1 minute for the recently active posts, scales up logarithmically
  35. const COLORS = {
  36. question: {
  37. backRGB: '80, 133, 195',
  38. fore: '#265184',
  39. },
  40. answer: {
  41. backRGB: '112, 195, 80',
  42. fore: '#3f7722',
  43. foreInv: 'white',
  44. },
  45. deleted: {
  46. backRGB: '181, 103, 103',
  47. fore: 'rgb(181, 103, 103)',
  48. foreInv: 'white',
  49. },
  50. closed: {
  51. backRGB: '255, 206, 93',
  52. fore: 'rgb(204, 143, 0)',
  53. foreInv: 'white',
  54. },
  55. };
  56.  
  57. let xhr;
  58. let preview = {
  59. frame: null,
  60. link: null,
  61. hover: {x:0, y:0},
  62. timer: 0,
  63. cacheCSS: {},
  64. stylesOverride: '',
  65. };
  66.  
  67. const rxPreviewable = getURLregexForMatchedSites();
  68. const thisPageUrls = getPageBaseUrls(location.href);
  69.  
  70. initStyles();
  71. initPolyfills();
  72. setMutationHandler('a', onLinkAdded, {processExisting: true});
  73. setTimeout(cleanupCache, 10000);
  74.  
  75. /**************************************************************/
  76.  
  77. function onLinkAdded(links) {
  78. for (let i = 0, link; (link = links[i++]); ) {
  79. if (isLinkPreviewable(link)) {
  80. link.removeAttribute('title');
  81. $on('mouseover', link, onLinkHovered);
  82. }
  83. }
  84. }
  85.  
  86. function onLinkHovered(e) {
  87. if (e.ctrlKey || e.altKey || e.shiftKey || e.metaKey)
  88. return;
  89. preview.link = this;
  90. $on('mousemove', this, onLinkMouseMove);
  91. $on('mouseout', this, abortPreview);
  92. $on('mousedown', this, abortPreview);
  93. restartPreviewTimer(this);
  94. }
  95.  
  96. function onLinkMouseMove(e) {
  97. let stoppedMoving = Math.abs(preview.hover.x - e.clientX) < 2 &&
  98. Math.abs(preview.hover.y - e.clientY) < 2;
  99. if (!stoppedMoving)
  100. return;
  101. preview.hover.x = e.clientX;
  102. preview.hover.y = e.clientY;
  103. restartPreviewTimer(this);
  104. }
  105.  
  106. function restartPreviewTimer(link) {
  107. clearTimeout(preview.timer);
  108. preview.timer = setTimeout(() => {
  109. preview.timer = 0;
  110. $off('mousemove', link, onLinkMouseMove);
  111. if (link.matches(':hover'))
  112. downloadPreview(link.href);
  113. }, PREVIEW_DELAY);
  114. }
  115.  
  116. function abortPreview(e) {
  117. releaseLinkListeners(this);
  118. preview.timer = setTimeout(link => {
  119. if (link == preview.link && preview.frame && !preview.frame.matches(':hover')) {
  120. releaseLinkListeners(link);
  121. preview.frame.contentWindow.postMessage('SEpreview-hidden', '*');
  122. fadeOut(preview.frame);
  123. }
  124. }, PREVIEW_DELAY * 3, this);
  125. if (xhr)
  126. xhr.abort();
  127. }
  128.  
  129. function releaseLinkListeners(link) {
  130. $off('mousemove', link, onLinkMouseMove);
  131. $off('mouseout', link, abortPreview);
  132. $off('mousedown', link, abortPreview);
  133. clearTimeout(preview.timer);
  134. }
  135.  
  136. function fadeOut(element, transition) {
  137. if (transition) {
  138. element.style.transition = typeof transition == 'number' ? `opacity ${transition}s ease-in-out` : transition;
  139. return setTimeout(fadeOut, 0, element);
  140. }
  141. element.style.opacity = 0;
  142. $on('transitionend', element, function remove() {
  143. $off('transitionend', element, remove);
  144. if (+element.style.opacity === 0)
  145. element.style.display = 'none';
  146. });
  147. }
  148.  
  149. function downloadPreview(url) {
  150. let cached = readCache(url);
  151. if (cached)
  152. showPreview(cached);
  153. else {
  154. xhr = GM_xmlhttpRequest({
  155. method: 'GET',
  156. url: httpsUrl(url),
  157. onload: r => {
  158. let html = r.responseText;
  159. let lastActivity = showPreview({finalUrl: r.finalUrl, html});
  160. let inactiveDays = Math.max(0, (Date.now() - lastActivity) / (24 * 3600 * 1000));
  161. let cacheDuration = CACHE_DURATION * Math.pow(Math.log(inactiveDays + 1) + 1, 2);
  162. writeCache({url, finalUrl: r.finalUrl, html, cacheDuration});
  163. },
  164. });
  165. }
  166. }
  167.  
  168. function showPreview({finalUrl, html, doc}) {
  169. doc = doc || new DOMParser().parseFromString(html, 'text/html');
  170. if (!doc || !doc.head) {
  171. error('no HEAD in the document received for', finalUrl);
  172. return;
  173. }
  174.  
  175. if (!$('base', doc))
  176. doc.head.insertAdjacentHTML('afterbegin', `<base href="${finalUrl}">`);
  177.  
  178. const answerIdMatch = finalUrl.match(/questions\/\d+\/[^\/]+\/(\d+)/);
  179. const isQuestion = !answerIdMatch;
  180. const postId = answerIdMatch ? '#answer-' + answerIdMatch[1] : '#question';
  181. const post = $(postId + ' .post-text', doc);
  182. if (!post)
  183. return error('No parsable post found', doc);
  184. const isDeleted = post.closest('.deleted-answer');
  185. const title = $('meta[property="og:title"]', doc).content;
  186. const status = isQuestion && !$('.question-status', post) && $('.question-status', doc);
  187. const isClosed = $('.question-originals-of-duplicate, .close-as-off-topic-status-list, .close-status-suffix', doc);
  188. const comments = $(`${postId} .comments`, doc);
  189. const commentsHidden = +$('tbody', comments).dataset.remainingCommentsCount;
  190. const commentsShowLink = commentsHidden && $(`${postId} .js-show-link.comments-link`, doc);
  191. const finalUrlOfQuestion = getCacheableUrl(finalUrl);
  192.  
  193. const lastActivity = +doc.body.getAttribute('SEpreview-lastActivity')
  194. || tryCatch(() => new Date($('.lastactivity-link', doc).title).getTime())
  195. || Date.now();
  196. if (lastActivity)
  197. doc.body.setAttribute('SEpreview-lastActivity', lastActivity);
  198.  
  199. $$remove('script', doc);
  200.  
  201. // underline previewable links
  202. for (let link of $$('a:not(.SEpreviewable)', doc)) {
  203. if (rxPreviewable.test(link.href)) {
  204. link.removeAttribute('title');
  205. link.classList.add('SEpreviewable');
  206. }
  207. }
  208.  
  209. if (!preview.frame) {
  210. preview.frame = document.createElement('iframe');
  211. preview.frame.id = 'SEpreview';
  212. document.body.appendChild(preview.frame);
  213. }
  214.  
  215. let pvDoc, pvWin;
  216. preview.frame.setAttribute('SEpreview-type',
  217. isDeleted ? 'deleted' : isQuestion ? (isClosed ? 'closed' : 'question') : 'answer');
  218. onFrameReady(preview.frame).then(
  219. () => {
  220. pvDoc = preview.frame.contentDocument;
  221. pvWin = preview.frame.contentWindow;
  222. initPolyfills(pvWin);
  223. })
  224. .then(addStyles)
  225. .then(render)
  226. .then(show);
  227. return lastActivity;
  228.  
  229. function addStyles() {
  230. const SEpreviewStyles = $replaceOrCreate({
  231. id: 'SEpreviewStyles',
  232. tag: 'style', parent: pvDoc.head, className: 'SEpreview-reuse',
  233. innerHTML: preview.stylesOverride,
  234. });
  235.  
  236. $replaceOrCreate($$('style, link[rel="stylesheet"]', doc).map(e =>
  237. e.localName == 'style' ? {
  238. id: 'SEpreview' + e.innerHTML.replace(/\W+/g, '').length,
  239. tag: 'style', before: SEpreviewStyles, className: 'SEpreview-reuse',
  240. innerHTML: e.innerHTML,
  241. } : {
  242. id: e.href.replace(/\W+/g, ''),
  243. tag: 'link', before: SEpreviewStyles, className: 'SEpreview-reuse',
  244. href: e.href, rel: 'stylesheet',
  245. })
  246. );
  247.  
  248. return onStyleSheetsReady($$('link[rel="stylesheet"]', pvDoc));
  249. }
  250.  
  251. function render() {
  252. pvDoc.body.setAttribute('SEpreview-type', preview.frame.getAttribute('SEpreview-type'));
  253.  
  254. $replaceOrCreate([{
  255. // title
  256. id: 'SEpreview-title', tag: 'a',
  257. parent: pvDoc.body, className: 'SEpreviewable',
  258. href: finalUrlOfQuestion,
  259. textContent: title,
  260. }, {
  261. // vote count, date, views#
  262. id: 'SEpreview-meta',
  263. parent: pvDoc.body,
  264. innerHTML: [
  265. renderVotes(post.closest('table')).replace(/(\S+) (\S+)/, '<b>$1</b> $2, '),
  266. isQuestion
  267. ? $$('#qinfo tr', doc)
  268. .map(row => $$('.label-key', row).map($text).join(' '))
  269. .join(', ').replace(/^((.+?) (.+?), .+?), .+? \3$/, '$1')
  270. : [...$$('.user-action-time', post.closest('.answer'))]
  271. .reverse().map($text).join(', ')
  272. ].join('')
  273. }, {
  274. // content wrapper
  275. id: 'SEpreview-body',
  276. parent: pvDoc.body,
  277. className: isDeleted ? 'deleted-answer' : '',
  278. children: [post.parentElement, comments, commentsShowLink, status],
  279. }]);
  280.  
  281. renderCode();
  282.  
  283. // render bottom shelf
  284. const answers = $$('.answer', doc);
  285. if (answers.length > (isQuestion ? 0 : 1)) {
  286. $replaceOrCreate({
  287. id: 'SEpreview-answers',
  288. parent: pvDoc.body,
  289. innerHTML: 'Answers:&nbsp;' + answers.map(renderShelfAnswer).join(''),
  290. });
  291. } else
  292. $$remove('#SEpreview-answers', pvDoc);
  293.  
  294. // cleanup leftovers from previously displayed post and foreign elements not injected by us
  295. $$('style, link, body script, html > *:not(head):not(body)', pvDoc).forEach(e => {
  296. if (e.classList.contains('SEpreview-reuse'))
  297. e.classList.remove('SEpreview-reuse');
  298. else
  299. e.remove();
  300. });
  301. }
  302.  
  303. function renderCode() {
  304. const codeBlocks = $$('pre code', pvDoc);
  305. if (codeBlocks.length) {
  306. codeBlocks.forEach(e => e.parentElement.classList.add('prettyprint'));
  307. if (!pvWin.StackExchange) {
  308. pvWin.StackExchange = {};
  309. let script = $scriptIn(pvDoc.head);
  310. script.text = 'StackExchange = {}';
  311. script = $scriptIn(pvDoc.head);
  312. script.src = 'https://cdn.sstatic.net/Js/prettify-full.en.js';
  313. script.setAttribute('onload', 'prettyPrint()');
  314. } else
  315. $scriptIn(pvDoc.body).text = 'prettyPrint()';
  316. }
  317. }
  318.  
  319. function renderShelfAnswer(e, index) {
  320. const shortUrl = $('.short-link', e).href.replace(/(\d+)\/\d+/, '$1');
  321. const extraClasses = (e.matches(postId) ? ' SEpreviewed' : '') +
  322. (e.matches('.deleted-answer') ? ' deleted-answer' : '');
  323. const author = $('.post-signature:last-child', e);
  324. return `<a href="${shortUrl}"
  325. SEpreview-fullUrl="${finalUrlOfQuestion + '/' + shortUrl.match(/\/(\d+)/)[1]}"
  326. title="${$text('.user-details a', author) +
  327. ' (rep '+$text('.reputation-score', author) + ')\n' +
  328. $text('.user-action-time', author) +
  329. renderVotes(author)}"
  330. class="SEpreviewable${extraClasses}"
  331. >${index + 1}</a>`;
  332. }
  333.  
  334. function renderVotes(post) {
  335. return $text('.vote-count-post', post)
  336. .replace(/-?\d+/, s => s == '0' ? '' : '\n' + s + ' vote' + (+s > 1 ? 's' : ''));
  337. }
  338.  
  339. function show() {
  340. pvDoc.onmouseover = retainMainScrollPos;
  341. pvDoc.onclick = interceptLinks;
  342. pvWin.onmessage = e => {
  343. if (e.data == 'SEpreview-hidden') {
  344. pvWin.onmessage = null;
  345. pvDoc.onmouseover = null;
  346. pvDoc.onclick = null;
  347. }
  348. };
  349.  
  350. $('#SEpreview-body', pvDoc).scrollTop = 0;
  351. preview.frame.style.opacity = 1;
  352. preview.frame.style.display = '';
  353. }
  354.  
  355. function retainMainScrollPos(e) {
  356. let scrollPos = {x:scrollX, y:scrollY};
  357. $on('scroll', preventScroll);
  358. $on('mouseover', releaseScrollLock);
  359.  
  360. function preventScroll(e) {
  361. scrollTo(scrollPos.x, scrollPos.y);
  362. }
  363.  
  364. function releaseScrollLock(e) {
  365. $off('mouseout', releaseScrollLock);
  366. $off('scroll', preventScroll);
  367. }
  368. }
  369.  
  370. function interceptLinks(e) {
  371. const link = e.target.closest('a');
  372. if (!link)
  373. return;
  374. if (link.matches('.js-show-link.comments-link')) {
  375. fadeOut(link, 0.5);
  376. loadComments();
  377. }
  378. else if (e.button || e.ctrlKey || e.altKey || e.shiftKey || e.metaKey || !link.matches('.SEpreviewable'))
  379. return (link.target = '_blank');
  380. else if (link.matches('#SEpreview-answers a, a#SEpreview-title'))
  381. showPreview({
  382. finalUrl: link.getAttribute('SEpreview-fullUrl') || link.href,
  383. doc
  384. });
  385. else
  386. downloadPreview(link.getAttribute('SEpreview-fullUrl') || link.href);
  387. e.preventDefault();
  388. }
  389.  
  390. function loadComments() {
  391. GM_xmlhttpRequest({
  392. method: 'GET',
  393. url: new URL(finalUrl).origin + '/posts/' + comments.id.match(/\d+/)[0] + '/comments',
  394. onload: r => {
  395. let tbody = $(`#${comments.id} tbody`, pvDoc);
  396. let oldIds = new Set([...tbody.rows].map(e => e.id));
  397. tbody.innerHTML = r.responseText;
  398. for (let tr of tbody.rows)
  399. if (!oldIds.has(tr.id))
  400. tr.classList.add('new-comment-highlight');
  401. },
  402. });
  403. }
  404. }
  405.  
  406. function getCacheableUrl(url) {
  407. // strips querys and hashes and anything after the main part https://site/questions/####/title/
  408. return url
  409. .replace(/(\/q(?:uestions)?\/\d+\/[^\/]+).*/, '$1')
  410. .replace(/(\/a(?:nswers)?\/\d+).*/, '$1')
  411. .replace(/[?#].*$/, '');
  412. }
  413.  
  414. function readCache(url) {
  415. keyUrl = getCacheableUrl(url);
  416. const meta = (localStorage[keyUrl] || '').split('\t');
  417. const expired = +meta[0] < Date.now();
  418. const finalUrl = meta[1] || url;
  419. const keyFinalUrl = meta[1] ? getCacheableUrl(finalUrl) : keyUrl;
  420. return !expired && {
  421. finalUrl,
  422. html: LZString.decompressFromUTF16(localStorage[keyFinalUrl + '\thtml']),
  423. };
  424. }
  425.  
  426. function writeCache({url, finalUrl, html, cacheDuration = CACHE_DURATION, cleanupRetry}) {
  427. // keyUrl=expires
  428. // redirected keyUrl=expires+finalUrl, and an additional entry keyFinalUrl=expires is created
  429. // keyFinalUrl\thtml=html
  430. cacheDuration = Math.max(CACHE_DURATION, Math.min(0xDEADBEEF, Math.floor(cacheDuration)));
  431. finalUrl = finalUrl.replace(/[?#].*/, '');
  432. const keyUrl = getCacheableUrl(url);
  433. const keyFinalUrl = getCacheableUrl(finalUrl);
  434. const expires = Date.now() + cacheDuration;
  435. if (!tryCatch(() => localStorage[keyFinalUrl + '\thtml'] = LZString.compressToUTF16(html))) {
  436. if (cleanupRetry)
  437. return error('localStorage write error');
  438. cleanupCache({aggressive: true});
  439. setIimeout(writeCache, 0, {url, finalUrl, html, cacheDuration, cleanupRetry: true});
  440. }
  441. localStorage[keyFinalUrl] = expires;
  442. if (keyUrl != keyFinalUrl)
  443. localStorage[keyUrl] = expires + '\t' + finalUrl;
  444. setTimeout(() => {
  445. [keyUrl, keyFinalUrl, keyFinalUrl + '\thtml'].forEach(e => localStorage.removeItem(e));
  446. }, cacheDuration + 1000);
  447. }
  448.  
  449. function cleanupCache({aggressive = false} = {}) {
  450. Object.keys(localStorage).forEach(k => {
  451. if (k.match(/^https?:\/\/[^\t]+$/)) {
  452. let meta = (localStorage[k] || '').split('\t');
  453. if (+meta[0] > Date.now() && !aggressive)
  454. return;
  455. if (meta[1])
  456. localStorage.removeItem(meta[1]);
  457. localStorage.removeItem(`${meta[1] || k}\thtml`);
  458. localStorage.removeItem(k);
  459. }
  460. });
  461. }
  462.  
  463. function onFrameReady(frame) {
  464. if (frame.contentDocument.readyState == 'complete')
  465. return Promise.resolve();
  466. else
  467. return new Promise(resolve => {
  468. $on('load', frame, function onLoad() {
  469. $off('load', frame, onLoad);
  470. resolve();
  471. });
  472. });
  473. }
  474.  
  475. function onStyleSheetsReady(linkElements) {
  476. return new Promise(function retry(resolve) {
  477. if (linkElements.every(e => e.sheet && e.sheet.href == e.href))
  478. resolve();
  479. else
  480. setTimeout(retry, 0, resolve);
  481. });
  482. }
  483.  
  484. function getURLregexForMatchedSites() {
  485. return new RegExp('https?://(\\w*\\.)*(' + GM_info.script.matches.map(m =>
  486. m.match(/^.*?\/\/\W*(\w.*?)\//)[1].replace(/\./g, '\\.')
  487. ).join('|') + ')/(questions|q|a)/\\d+');
  488. }
  489.  
  490. function isLinkPreviewable(link) {
  491. const inPreview = link.ownerDocument != document;
  492. if (!rxPreviewable.test(link.href) || link.matches('.short-link'))
  493. return false;
  494. const pageUrls = inPreview ? getPageBaseUrls(preview.link.href) : thisPageUrls;
  495. const url = httpsUrl(link.href);
  496. return !url.startsWith(pageUrls.base) &&
  497. !url.startsWith(pageUrls.short);
  498. }
  499.  
  500. function getPageBaseUrls(url) {
  501. const base = httpsUrl((url.match(rxPreviewable) || [])[0]);
  502. return base ? {
  503. base,
  504. short: base.replace('/questions/', '/q/'),
  505. } : {};
  506. }
  507.  
  508. function httpsUrl(url) {
  509. return (url || '').replace(/^http:/, 'https:');
  510. }
  511.  
  512. function $(selector, node = document) {
  513. return node.querySelector(selector);
  514. }
  515.  
  516. function $$(selector, node = document) {
  517. return node.querySelectorAll(selector);
  518. }
  519.  
  520. function $text(selector, node = document) {
  521. const e = typeof selector == 'string' ? node.querySelector(selector) : selector;
  522. return e ? e.textContent.trim() : '';
  523. }
  524.  
  525. function $$remove(selector, node = document) {
  526. node.querySelectorAll(selector).forEach(e => e.remove());
  527. }
  528.  
  529. function $appendChildren(newParent, elements) {
  530. const doc = newParent.ownerDocument;
  531. for (let e of elements)
  532. if (e)
  533. newParent.appendChild(e.ownerDocument == doc ? e : doc.importNode(e, true));
  534. }
  535.  
  536. function $replaceOrCreate(options) {
  537. if (options.length && typeof options[0] == 'object')
  538. return [].map.call(options, $replaceOrCreate);
  539. const doc = (options.parent || options.before).ownerDocument;
  540. const el = doc.getElementById(options.id) || doc.createElement(options.tag || 'div');
  541. for (let key of Object.keys(options)) {
  542. switch (key) {
  543. case 'tag':
  544. case 'parent':
  545. case 'before':
  546. break;
  547. case 'children':
  548. if (el.children.length)
  549. el.innerHTML = '';
  550. $appendChildren(el, options[key]);
  551. break;
  552. default:
  553. const value = options[key];
  554. if (key in el && el[key] != value)
  555. el[key] = value;
  556. }
  557. }
  558. if (!el.parentElement)
  559. (options.parent || options.before.parentElement).insertBefore(el, options.before);
  560. return el;
  561. }
  562.  
  563. function $scriptIn(element) {
  564. return element.appendChild(element.ownerDocument.createElement('script'));
  565. }
  566.  
  567. function $on(eventName, ...args) {
  568. // eventName, selector, node, callback, options
  569. // eventName, selector, callback, options
  570. // eventName, node, callback, options
  571. // eventName, callback, options
  572. const selector = typeof args[0] == 'string' ? args[0] : null;
  573. const node = args[0] instanceof Node ? args[0] : args[1] instanceof Node ? args[1] : document;
  574. const callback = args[typeof args[0] == 'function' ? 0 : typeof args[1] == 'function' ? 1 : 2];
  575. const options = args[args.length - 1] != callback ? args[args.length - 1] : undefined;
  576. const method = this == 'removeEventListener' ? this : 'addEventListener';
  577. (selector ? node.querySelector(selector) : node)[method](eventName, callback, options);
  578. }
  579.  
  580. function $off(eventName, ...args) {
  581. $on.apply('removeEventListener', arguments);
  582. }
  583.  
  584. function log(...args) {
  585. console.log(GM_info.script.name, ...args);
  586. }
  587.  
  588. function error(...args) {
  589. console.error(GM_info.script.name, ...args);
  590. }
  591.  
  592. function tryCatch(fn) {
  593. try { return fn() }
  594. catch(e) {}
  595. }
  596.  
  597. function initPolyfills(context = window) {
  598. for (let method of ['forEach', 'filter', 'map', 'every', context.Symbol.iterator])
  599. if (!context.NodeList.prototype[method])
  600. context.NodeList.prototype[method] = context.Array.prototype[method];
  601. }
  602.  
  603. function initStyles() {
  604. GM_addStyle(`
  605. #SEpreview {
  606. all: unset;
  607. box-sizing: content-box;
  608. width: 720px; /* 660px + 30px + 30px */
  609. height: 33%;
  610. min-height: 200px;
  611. position: fixed;
  612. opacity: 0;
  613. transition: opacity .5s cubic-bezier(.88,.02,.92,.66);
  614. right: 0;
  615. bottom: 0;
  616. padding: 0;
  617. margin: 0;
  618. background: white;
  619. box-shadow: 0 0 100px rgba(0,0,0,0.5);
  620. z-index: 999999;
  621. border-width: 8px;
  622. border-style: solid;
  623. }
  624. `
  625. + Object.keys(COLORS).map(s => `
  626. #SEpreview[SEpreview-type="${s}"] {
  627. border-color: rgb(${COLORS[s].backRGB});
  628. }
  629. `).join('')
  630. );
  631.  
  632. preview.stylesOverride = `
  633. body, html {
  634. min-width: unset!important;
  635. box-shadow: none!important;
  636. padding: 0!important;
  637. margin: 0!important;
  638. }
  639. html, body {
  640. background: unset!important;;
  641. }
  642. body {
  643. display: flex;
  644. flex-direction: column;
  645. height: 100vh;
  646. }
  647. a.SEpreviewable:not(#SEpreview-title) {
  648. text-decoration: underline !important;
  649. }
  650. #SEpreview-title {
  651. all: unset;
  652. display: block;
  653. padding: 20px 30px;
  654. font-weight: bold;
  655. font-size: 18px;
  656. line-height: 1.2;
  657. cursor: pointer;
  658. }
  659. #SEpreview-title:hover {
  660. text-decoration: underline;
  661. }
  662. #SEpreview-meta {
  663. position: absolute;
  664. top: .5ex;
  665. left: 30px;
  666. opacity: 0.5;
  667. }
  668. #SEpreview-title:hover + #SEpreview-meta {
  669. opacity: 1.0;
  670. }
  671.  
  672. #SEpreview-body {
  673. padding: 30px!important;
  674. overflow: auto;
  675. flex-grow: 2;
  676. }
  677. #SEpreview-body .post-menu {
  678. display: none!important;
  679. }
  680. #SEpreview-body > .question-status {
  681. margin: -10px -30px -30px;
  682. padding-left: 30px;
  683. }
  684. #SEpreview-body > .question-status h2 {
  685. font-weight: normal;
  686. }
  687. #SEpreview-body > a + .question-status {
  688. margin-top: 20px;
  689. }
  690.  
  691. #SEpreview-answers {
  692. all: unset;
  693. display: block;
  694. padding: 10px 30px;
  695. font-weight: bold;
  696. font-size: 20px;
  697. line-height: 1.3;
  698. border-top: 4px solid rgba(${COLORS.answer.backRGB}, 0.37);
  699. background-color: rgba(${COLORS.answer.backRGB}, 0.37);
  700. color: ${COLORS.answer.fore};
  701. word-break: break-word;
  702. }
  703. #SEpreview-answers a {
  704. color: ${COLORS.answer.fore};
  705. padding: .25ex .75ex;
  706. text-decoration: none;
  707. }
  708. #SEpreview-answers a.deleted-answer {
  709. color: ${COLORS.deleted.fore};
  710. background: transparent;
  711. }
  712. #SEpreview-answers a:hover:not(.SEpreviewed) {
  713. text-decoration: underline;
  714. }
  715. #SEpreview-answers a.SEpreviewed {
  716. background-color: ${COLORS.answer.fore};
  717. color: ${COLORS.answer.foreInv};
  718. }
  719.  
  720. .delete-tag,
  721. .comment-actions td:last-child {
  722. display: none;
  723. }
  724. .comments .new-comment-highlight {
  725. -webkit-animation: highlight 9s cubic-bezier(0,.8,.37,.88);
  726. -moz-animation: highlight 9s cubic-bezier(0,.8,.37,.88);
  727. animation: highlight 9s cubic-bezier(0,.8,.37,.88);
  728. }
  729.  
  730. @-webkit-keyframes highlight {
  731. from {background-color: #ffcf78}
  732. to {background-color: none}
  733. }
  734. `
  735. + Object.keys(COLORS).map(s => `
  736. body[SEpreview-type="${s}"] #SEpreview-title {
  737. background-color: rgba(${COLORS[s].backRGB}, 0.37);
  738. color: ${COLORS[s].fore};
  739. }
  740. body[SEpreview-type="${s}"] #SEpreview-body::-webkit-scrollbar {
  741. background-color: rgba(${COLORS[s].backRGB}, 0.1); }
  742. body[SEpreview-type="${s}"] #SEpreview-body::-webkit-scrollbar-thumb {
  743. background-color: rgba(${COLORS[s].backRGB}, 0.2); }
  744. body[SEpreview-type="${s}"] #SEpreview-body::-webkit-scrollbar-thumb:hover {
  745. background-color: rgba(${COLORS[s].backRGB}, 0.3); }
  746. body[SEpreview-type="${s}"] #SEpreview-body::-webkit-scrollbar-thumb:active {
  747. background-color: rgba(${COLORS[s].backRGB}, 0.75); }
  748. `).join('')
  749. + ['deleted', 'closed'].map(s => `
  750. body[SEpreview-type="${s}"] #SEpreview-answers {
  751. border-top-color: rgba(${COLORS[s].backRGB}, 0.37);
  752. background-color: rgba(${COLORS[s].backRGB}, 0.37);
  753. color: ${COLORS[s].fore};
  754. }
  755. body[SEpreview-type="${s}"] #SEpreview-answers a.SEpreviewed {
  756. background-color: ${COLORS[s].fore};
  757. color: ${COLORS[s].foreInv};
  758. }
  759. `).join('')
  760. }