Greasy Fork 还支持 简体中文。

viewsource

Viewsource for Firefox, like Chrome

目前為 2016-01-13 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name viewsource
  3. // @namespace devs.forumvi.com
  4. // @description Viewsource for Firefox, like Chrome
  5. // @include *
  6. // @version 2.1.0
  7. // @author Zzbaivong
  8. // @resource light https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/github-gist.min.css
  9. // @resource dark https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/monokai-sublime.min.css
  10. // @require https://openuserjs.org/src/libs/baivong/beautify-html.js
  11. // @require https://openuserjs.org/src/libs/baivong/beautify-js.min.js
  12. // @require https://openuserjs.org/src/libs/baivong/beautify-css.min.js
  13. // @require https://openuserjs.org/src/libs/baivong/highlight-xml.js
  14. // @run-at doc-end
  15. // @grant GM_addStyle
  16. // @grant GM_getResourceText
  17. // @grant GM_xmlhttpRequest
  18. // @grant GM_registerMenuCommand
  19. // ==/UserScript==
  20.  
  21. (function() {
  22.  
  23. "use strict";
  24.  
  25. var theme = "light", // light|dark
  26.  
  27. urlpage = window.top.location.href,
  28. doc = document;
  29.  
  30. function viewsource() {
  31. GM_xmlhttpRequest({
  32. method: "GET",
  33. url: urlpage,
  34. onload: function(response) {
  35.  
  36. var txt = html_beautify(response.response),
  37. content = doc.body;
  38.  
  39. doc.head.innerHTML = "";
  40. content.innerHTML = "";
  41. content.removeAttribute("id");
  42. content.removeAttribute("class");
  43. content.removeAttribute("style");
  44. content.removeAttribute("onload");
  45. doc.title = "view-source:" + urlpage;
  46.  
  47. GM_addStyle(GM_getResourceText(theme) + "html,body,pre{margin:0;padding:0}.hljs{white-space:pre;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}#scroll-x{position:fixed;right:0;top:0;width:120px;cursor:w-resize;z-index:999;background:transparent;bottom:0}");
  48.  
  49. var output = doc.createElement("PRE");
  50. output.setAttribute("class", "xml");
  51. output.textContent = txt;
  52.  
  53. content.appendChild(output);
  54.  
  55. hljs.highlightBlock(output);
  56.  
  57. var lines = txt.split("\n"),
  58. l = "";
  59. lines = lines ? lines.length : 0;
  60. for (var i = 0; i < lines; i++) {
  61. l += (i + 1) + "\n";
  62. }
  63.  
  64. output.setAttribute("data-lines", l);
  65.  
  66. var node = doc.createElement("DIV");
  67. node.id = "scroll-x";
  68. content.appendChild(node);
  69.  
  70. node.onwheel = function(e) {
  71. e.preventDefault();
  72. output.scrollLeft += (e.deltaY * 10);
  73. return false;
  74. };
  75.  
  76. var attrUrl = doc.getElementsByClassName('hljs-attr');
  77. for (var j = 0; j < attrUrl.length; j++) {
  78. if (/\b(src|href\b)/.test(attrUrl[j].textContent)) {
  79. var link = attrUrl[j].nextSibling.nextSibling;
  80. var url = link.textContent.slice(1, -1);
  81. link.innerHTML = '"<a href="' + url + '" target="_blank">' + url + '</a>"';
  82. }
  83. }
  84.  
  85. }
  86. });
  87. }
  88.  
  89. GM_registerMenuCommand("View source", viewsource, "m");
  90.  
  91. if (doc.contentType === "text/html" && doc.URL === urlpage) {
  92. window.onkeydown = function(e) {
  93.  
  94. // Ctrl + M
  95. if (e.which == 77 && e.ctrlKey) {
  96. e.preventDefault();
  97.  
  98. viewsource();
  99. }
  100. };
  101. }
  102. }());