viewsource

Viewsource for Firefox, like Chrome

当前为 2016-04-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name viewsource
  3. // @namespace devs.forumvi.com
  4. // @description Viewsource for Firefox, like Chrome
  5. // @version 2.3.4
  6. // @icon http://i.imgur.com/6yZMOeH.png
  7. // @author Zzbaivong
  8. // @resource light https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/styles/github-gist.min.css
  9. // @resource dark https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/styles/monokai-sublime.min.css
  10. // @require https://greasyfork.org/scripts/18530-beautify-html/code/beautify-html.js?version=117787
  11. // @require https://greasyfork.org/scripts/18531-beautify-js/code/beautify-js.js?version=117786
  12. // @require https://greasyfork.org/scripts/18528-beautify-css/code/beautify-css.js?version=117789
  13. // @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/highlight.min.js
  14. // @noframes
  15. // @supportURL https://github.com/baivong/Userscript/issues
  16. // @run-at document-end
  17. // @grant GM_addStyle
  18. // @grant GM_getResourceText
  19. // @grant GM_xmlhttpRequest
  20. // @grant GM_registerMenuCommand
  21. // ==/UserScript==
  22.  
  23. (function () {
  24.  
  25. 'use strict';
  26.  
  27. var theme = 'light', // light|dark
  28.  
  29. win = window,
  30. urlpage = win.top.location.href,
  31. doc = document,
  32. wrapcontent = doc.documentElement,
  33. content = doc.body;
  34.  
  35. function scrollByDragging(container, disableH, disableV) {
  36.  
  37. function mouseUp(e) {
  38. if (e.which !== 3) return;
  39.  
  40. window.removeEventListener('mousemove', mouseMove, true);
  41. container.style.cursor = 'default';
  42. }
  43.  
  44. function mouseDown(e) {
  45. if (e.which !== 3) return;
  46.  
  47. pos = {
  48. x: e.clientX,
  49. y: e.clientY
  50. };
  51.  
  52. window.addEventListener('mousemove', mouseMove, true);
  53. container.style.cursor = 'move';
  54. }
  55.  
  56. function mouseMove(e) {
  57. if (!disableH) container.scrollLeft -= (-pos.x + (pos.x = e.clientX));
  58. if (!disableV) container.scrollTop -= (-pos.y + (pos.y = e.clientY));
  59. }
  60.  
  61. var pos = {
  62. x: 0,
  63. y: 0
  64. };
  65.  
  66. container.oncontextmenu = function (e) {
  67. e.preventDefault();
  68. };
  69.  
  70. container.addEventListener('mousedown', mouseDown, false);
  71. window.addEventListener('mouseup', mouseUp, false);
  72.  
  73. }
  74.  
  75. function removeEvents(ele, attr) {
  76. 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(' '),
  77. x;
  78. for (x in events) {
  79. var _event = events[x];
  80. ele[_event] = null;
  81. if (attr) {
  82. ele.removeAttribute(_event);
  83. }
  84. }
  85. }
  86.  
  87. function viewsource() {
  88. GM_xmlhttpRequest({
  89. method: 'GET',
  90. url: urlpage,
  91. onload: function (response) {
  92.  
  93. removeEvents(win);
  94. removeEvents(doc);
  95. removeEvents(wrapcontent, true);
  96. removeEvents(content, true);
  97.  
  98. var txt = html_beautify(response.response);
  99.  
  100. doc.head.innerHTML = '';
  101. content.innerHTML = '';
  102. content.removeAttribute('id');
  103. content.removeAttribute('class');
  104. content.removeAttribute('style');
  105. doc.title = 'view-source:' + urlpage;
  106.  
  107. GM_addStyle(GM_getResourceText(theme) + 'html,body,pre{margin:0;padding:0}.hljs{overflow:hidden;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}');
  108.  
  109. var output = doc.createElement('PRE');
  110. output.setAttribute('class', 'xml');
  111. output.textContent = txt;
  112.  
  113. content.appendChild(output);
  114.  
  115. hljs.highlightBlock(output);
  116.  
  117. var lines = txt.split('\n'),
  118. l = '';
  119. lines = lines ? lines.length : 0;
  120. for (var i = 0; i < lines; i++) {
  121. l += (i + 1) + '\n';
  122. }
  123.  
  124. output.setAttribute('data-lines', l);
  125. //output.style.width = output.scrollWidth + 'px';
  126.  
  127. scrollByDragging(output, false, true);
  128. scrollByDragging(content, true);
  129. scrollByDragging(wrapcontent, true);
  130.  
  131. var attrUrl = doc.getElementsByClassName('hljs-attr');
  132. for (var j = 0; j < attrUrl.length; j++) {
  133. if (/\b(src|href\b)/.test(attrUrl[j].textContent)) {
  134. var link = attrUrl[j].nextSibling.nextSibling;
  135. var url = link.textContent.slice(1, -1);
  136. link.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>';
  137. }
  138. }
  139.  
  140. }
  141. });
  142. }
  143.  
  144. GM_registerMenuCommand('View source', viewsource, 'm');
  145.  
  146. if (doc.contentType === 'text/html' && doc.URL === urlpage) {
  147. win.onkeydown = function (e) {
  148.  
  149. // Ctrl + M
  150. if (e.which === 77 && e.ctrlKey) {
  151. e.preventDefault();
  152.  
  153. viewsource();
  154. }
  155. };
  156. }
  157. }());