viewsource

View and beautify page source. Shortcut: Alt+U.

安装此脚本?
作者推荐脚本

您可能也喜欢Javascript-css beautify

安装此脚本
  1. // ==UserScript==
  2. // @name viewsource
  3. // @name:vi viewsource
  4. // @namespace devs.forumvi.com
  5. // @description View and beautify page source. Shortcut: Alt+U.
  6. // @description:vi Định dạng và làm đẹp mã nguồn trang web. Phím tắt: Alt+U.
  7. // @version 3.3.0
  8. // @icon http://i.imgur.com/6yZMOeH.png
  9. // @author Zzbaivong
  10. // @oujs:author baivong
  11. // @license MIT; https://baivong.mit-license.org/license.txt
  12. // @match http://*/*
  13. // @match https://*/*
  14. // @resource js_beautify https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.12.0/beautify.min.js
  15. // @resource css_beautify https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.12.0/beautify-css.min.js
  16. // @resource html_beautify https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.12.0/beautify-html.min.js
  17. // @resource hljs https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js
  18. // @resource dark https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/atom-one-dark.min.css
  19. // @resource light https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/atom-one-light.min.css
  20. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?v=a834d46
  21. // @noframes
  22. // @connect *
  23. // @supportURL https://github.com/lelinhtinh/Userscript/issues
  24. // @run-at document-idle
  25. // @grant GM.getResourceUrl
  26. // @grant GM_getResourceURL
  27. // @grant GM_addStyle
  28. // @grant GM.xmlHttpRequest
  29. // @grant GM_xmlhttpRequest
  30. // @grant GM.openInTab
  31. // @grant GM_openInTab
  32. // @grant GM_registerMenuCommand
  33. // ==/UserScript==
  34.  
  35. /* eslint-env worker, es6 */
  36. (() => {
  37. 'use strict';
  38.  
  39. /**
  40. * Color themes
  41. * @type {String} dark|light
  42. */
  43. const STYLE = 'dark';
  44.  
  45. /* === DO NOT CHANGE === */
  46.  
  47. const urlpage = location.href;
  48.  
  49. if (!/^application\/(xhtml+xml|xml|rss+xml)|text\/(html|xml)$/.test(document.contentType)) return;
  50.  
  51. GM_registerMenuCommand('Beautify Page Source', viewsource, 'u');
  52. document.onkeydown = (e) => {
  53. if (e.which === 85 && e.altKey) {
  54. // Alt+U
  55. e.preventDefault();
  56. viewsource();
  57. }
  58. };
  59.  
  60. const handleURL = URL.createObjectURL(
  61. new Blob(
  62. [
  63. '(',
  64. function () {
  65. self.window = {};
  66.  
  67. self.onmessage = (e) => {
  68. var source = e.data.content;
  69.  
  70. importScripts(e.data.libs[0]);
  71. importScripts(e.data.libs[1]);
  72. importScripts(e.data.libs[2]);
  73. source = self.window.html_beautify(source, {
  74. indent_scripts: 'keep',
  75. });
  76.  
  77. importScripts(e.data.libs[3]);
  78. source = self.hljs.highlight('xml', source, true).value;
  79.  
  80. self.postMessage({
  81. base: e.data.base,
  82. theme: e.data.libs[4],
  83. source: source,
  84. });
  85. };
  86. }.toString(),
  87. ')()',
  88. ],
  89. {
  90. type: 'application/javascript',
  91. }
  92. )
  93. );
  94.  
  95. const worker = new Worker(handleURL);
  96. worker.onmessage = (e) => {
  97. if (!e.data) return;
  98.  
  99. const viewSourceURL = URL.createObjectURL(
  100. new Blob(
  101. [
  102. `<!DOCTYPE html>
  103. <html>
  104. <head>
  105. <meta charset="utf-8">
  106. <base href="${e.data.base}" target="_blank">
  107. <title>beautify-source:${urlpage}</title>
  108. <style>${e.data.theme}*{margin:0;padding:0}html{line-height:1em;background:#1d1f21;color:#c5c8c6}pre{white-space:pre-wrap;word-wrap:break-word;word-break:break-all}a{color:#4d4bd8}</style>
  109. </head>
  110. <body>
  111. <pre class="hljs xml">${e.data.source}</pre>
  112. <script>
  113. const attrUrl = document.getElementsByClassName('hljs-attr');
  114. for (let j = 0; j < attrUrl.length; j++) {
  115. if (/\\b(src|href\\b)/.test(attrUrl[j].textContent)) {
  116. let link = attrUrl[j].nextSibling.nextSibling,
  117. url = link.textContent,
  118. quote = url.slice(0, 1);
  119.  
  120. if (quote !== "'" && quote !== '"') {
  121. quote = '';
  122. } else {
  123. url = url.slice(1, -1);
  124. }
  125.  
  126. link.innerHTML = quote + '<a href="' + url + '" target="_blank">' + url + '</a>' + quote;
  127. }
  128. }
  129. </script>
  130. </body>
  131. </html>`,
  132. ],
  133. {
  134. type: 'text/html; charset=utf-8',
  135. }
  136. )
  137. );
  138.  
  139. GM.openInTab(urlpage, true);
  140. location.href = viewSourceURL;
  141. };
  142.  
  143. function viewsource() {
  144. const js_beautify = GM.getResourceUrl('js_beautify'),
  145. css_beautify = GM.getResourceUrl('css_beautify'),
  146. html_beautify = GM.getResourceUrl('html_beautify'),
  147. hljs = GM.getResourceUrl('hljs'),
  148. theme = GM.getResourceUrl(STYLE)
  149. .then(function (url) {
  150. return fetch(url);
  151. })
  152. .then(function (resp) {
  153. return resp.text();
  154. });
  155.  
  156. GM.xmlHttpRequest({
  157. method: 'GET',
  158. url: urlpage,
  159. onload: (response) => {
  160. const baseMatch = response.response.match(/<base[\s\n]+href[\s\n]*=[\s\n]*("|')?([^"'\s\n]+)("|')?.*?\/?>/i);
  161.  
  162. Promise.all([js_beautify, css_beautify, html_beautify, hljs, theme]).then((urls) => {
  163. worker.postMessage({
  164. libs: urls,
  165. base: baseMatch ? baseMatch[2] : urlpage.replace(/[^/]*$/, ''),
  166. content: response.response,
  167. });
  168. });
  169. },
  170. });
  171. }
  172. })();

QingJ © 2025

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