Greasy Fork 支持简体中文。

viewsource

View and beauty website source code. Support to see the source code by holding the right mouse and drag. Shortcut: Alt+U.

目前為 2017-05-15 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name viewsource
  3. // @namespace devs.forumvi.com
  4. // @description View and beauty website source code. Support to see the source code by holding the right mouse and drag. Shortcut: Alt+U.
  5. // @version 2.3.8
  6. // @icon http://i.imgur.com/6yZMOeH.png
  7. // @author Zzbaivong
  8. // @license MIT
  9. // @match http://*/*
  10. // @match https://*/*
  11. // @resource light https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.5.0/styles/github-gist.min.css
  12. // @resource dark https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.5.0/styles/monokai-sublime.min.css
  13. // @require https://greasyfork.org/scripts/18530-beautify-html/code/beautify-html.js?version=126855
  14. // @require https://greasyfork.org/scripts/18531-beautify-js/code/beautify-js.js?version=126854
  15. // @require https://greasyfork.org/scripts/18528-beautify-css/code/beautify-css.js?version=126856
  16. // @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.5.0/highlight.min.js
  17. // @noframes
  18. // @connect self
  19. // @supportURL https://github.com/baivong/Userscript/issues
  20. // @run-at document-idle
  21. // @grant GM_addStyle
  22. // @grant GM_getResourceText
  23. // @grant GM_xmlhttpRequest
  24. // @grant GM_registerMenuCommand
  25. // ==/UserScript==
  26.  
  27. (function () {
  28.  
  29. 'use strict';
  30.  
  31. var theme = 'light', // light|dark
  32.  
  33. win = window,
  34. urlpage = location.href,
  35. doc = document,
  36. wrapcontent = doc.documentElement,
  37. content = doc.body;
  38.  
  39. function scrollByDragging(container, disableH, disableV) {
  40.  
  41. function mouseUp(e) {
  42. if (e.which !== 3) return;
  43.  
  44. window.removeEventListener('mousemove', mouseMove, true);
  45. container.style.cursor = 'default';
  46. }
  47.  
  48. function mouseDown(e) {
  49. if (e.which !== 3) return;
  50.  
  51. pos = {
  52. x: e.clientX,
  53. y: e.clientY
  54. };
  55.  
  56. window.addEventListener('mousemove', mouseMove, true);
  57. container.style.cursor = 'move';
  58. }
  59.  
  60. function mouseMove(e) {
  61. if (!disableH) container.scrollLeft -= (-pos.x + (pos.x = e.clientX));
  62. if (!disableV) container.scrollTop -= (-pos.y + (pos.y = e.clientY));
  63. }
  64.  
  65. var pos = {
  66. x: 0,
  67. y: 0
  68. };
  69.  
  70. container.oncontextmenu = function (e) {
  71. e.preventDefault();
  72. };
  73.  
  74. container.addEventListener('mousedown', mouseDown, false);
  75. window.addEventListener('mouseup', mouseUp, false);
  76.  
  77. }
  78.  
  79. function removeEvents(ele, attr) {
  80. var events = 'onafterprint onbeforeprint onbeforeunload onerror onhashchange onload onmessage onoffline ononline onpagehide onpageshow onpopstate onresize onstorage onunload onblur onchange oncontextmenu onfocus oninput oninvalid onreset onsearch onselect onsubmit onkeydown onkeypress onkeyup onclick ondblclick ondrag ondragend ondragenter ondragleave ondragover ondragstart ondrop onmousedown onmousemove onmouseout onmouseover onmouseup onmousewheel onscroll onwheel oncopy oncut onpaste onerror onshow ontoggle'.split(' '),
  81. x;
  82. for (x in events) {
  83. var _event = events[x];
  84. ele[_event] = null;
  85. if (attr) {
  86. ele.removeAttribute(_event);
  87. }
  88. }
  89. }
  90.  
  91. function viewsource() {
  92. GM_xmlhttpRequest({
  93. method: 'GET',
  94. url: urlpage,
  95. onload: function (response) {
  96.  
  97. removeEvents(win);
  98. removeEvents(doc);
  99. removeEvents(wrapcontent, true);
  100. removeEvents(content, true);
  101.  
  102. var txt = html_beautify(response.response);
  103.  
  104. doc.head.innerHTML = '';
  105. content.innerHTML = '';
  106. content.removeAttribute('id');
  107. content.removeAttribute('class');
  108. content.removeAttribute('style');
  109. doc.title = 'view-source:' + urlpage;
  110.  
  111. GM_addStyle(GM_getResourceText(theme) + 'html,body,pre{margin:0;padding:0}.hljs{word-wrap:normal!important;white-space:pre!important;padding-left:4em;line-height:100%}.hljs::before{content:attr(data-lines);position:absolute;color:#d2d2d2;text-align:right;width:3.5em;left:-.5em;border-right:1px solid rgba(221, 221, 221, 0.36);padding-right:.5em}');
  112.  
  113. var output = doc.createElement('PRE');
  114. output.setAttribute('class', 'xml');
  115. output.textContent = txt;
  116.  
  117. content.appendChild(output);
  118.  
  119. hljs.highlightBlock(output);
  120.  
  121. var lines = txt.split('\n'),
  122. l = '';
  123. lines = lines ? lines.length : 0;
  124. for (var i = 0; i < lines; i++) {
  125. l += (i + 1) + '\n';
  126. }
  127.  
  128. output.setAttribute('data-lines', l);
  129. output.style.width = output.scrollWidth + 'px';
  130.  
  131. scrollByDragging(content);
  132. scrollByDragging(wrapcontent);
  133.  
  134. var attrUrl = doc.getElementsByClassName('hljs-attr');
  135. for (var j = 0; j < attrUrl.length; j++) {
  136. if (/\b(src|href\b)/.test(attrUrl[j].textContent)) {
  137. var link = attrUrl[j].nextSibling.nextSibling;
  138. var url = link.textContent.slice(1, -1);
  139. link.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>';
  140. }
  141. }
  142.  
  143. }
  144. });
  145. }
  146.  
  147. GM_registerMenuCommand('View source', viewsource, 'u');
  148.  
  149. if (/^application\/(xhtml+xml|xml|rss+xml)|text\/(html|xml)$/.test(doc.contentType)) {
  150. doc.onkeydown = function (e) {
  151.  
  152. // Alt+U
  153. if (e.which === 85 && e.altKey) {
  154. e.preventDefault();
  155.  
  156. viewsource();
  157. }
  158. };
  159. }
  160.  
  161. }());