Better GreasyFork Code Reader + JS Beautifier

Show the Codes page of any script on GreasyFork With all code lines background in white and beautify them if you want. With this script you can also Beautify your UserScripts before publishing them.

目前为 2021-01-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Better GreasyFork Code Reader + JS Beautifier
  3. // @namespace BetterGreasyCodeReader
  4. // @version 0.3
  5. // @description Show the Codes page of any script on GreasyFork With all code lines background in white and beautify them if you want. With this script you can also Beautify your UserScripts before publishing them.
  6. // @author hacker09
  7. // @include https://greasyfork.org/*/script_versions/new
  8. // @include https://greasyfork.org/*/scripts/*/versions/new
  9. // @icon https://www.google.com/s2/favicons?domain=greasyfork.org
  10. // @exclude https://greasyfork.org/*/script_versions/new?language=css
  11. // @include /^https:\/\/greasyfork\.org\/(?:[^\/]+\/)scripts\/(?:[^\/]+\/)code/
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.4/beautify.js
  13. // @run-at document-end
  14. // @grant none
  15. // @gnoiframes
  16. // ==/UserScript==
  17. (function() {
  18. 'use strict';
  19.  
  20. var JS_Beautifier_Options = { //Beginning of the "Your Selected Options (JSON):" Content
  21. "indent_size": "2",
  22. "indent_char": " ",
  23. "max_preserve_newlines": "5",
  24. "preserve_newlines": true,
  25. "keep_array_indentation": false,
  26. "break_chained_methods": false,
  27. "indent_scripts": "normal",
  28. "brace_style": "collapse",
  29. "space_before_conditional": true,
  30. "unescape_strings": false,
  31. "jslint_happy": false,
  32. "end_with_newline": false,
  33. "wrap_line_length": "0",
  34. "indent_inner_html": false,
  35. "comma_first": false,
  36. "e4x": false,
  37. "indent_empty_lines": false
  38. }; //End of the "Your Selected Options (JSON):" Content
  39.  
  40. document.querySelector("#script-feedback-suggestion") !== null ? document.querySelector("#script-feedback-suggestion").insertAdjacentHTML('beforeend', "<input type='checkbox' class='Beautify'><label>Beautify JS Codes</label>") : document.querySelector("label.checkbox-label").insertAdjacentHTML('afterEnd', "<input type='checkbox' class='Beautify'><label>Beautify JS Codes</label>"); //Add the input check box on the page
  41. var CodeBackup, UserScriptBackup, CodeTextElement, SourceEditorCheck;
  42.  
  43. document.querySelector("input.Beautify").onclick = function() { //When the checkbox is clicked
  44. if (document.querySelector("li.current").innerText === "Code") { //Run only on the Code page
  45. CodeTextElement = document.querySelector("ol.linenums").innerText; //Store the CodeTextElement to a variable
  46. SourceEditorCheck = true; //Define the SourceEditorCheck variable as true
  47. } //Finishes the if condition
  48.  
  49. if (location.href.match('versions/new') !== null) { //Run only on the Code page
  50.  
  51. document.querySelector("#enable-source-editor-code").onclick = function() { //When the checkbox is clicked
  52. if (document.querySelector("#enable-source-editor-code").checked === true) { //If the SourceEditor is enabled
  53. document.querySelector("input.Beautify").disabled = true; //Disable the Beautifier button
  54. } //Finishes the if condition
  55. else { //Starts the else condition
  56. document.querySelector("input.Beautify").disabled = false; //Enable the Beautifier button
  57. } //Finishes the else condition
  58. }; //Finishes the onlick listener
  59.  
  60. SourceEditorCheck = document.querySelector("#enable-source-editor-code").checked === false; //Define the SourceEditorCheck variable as false
  61. CodeTextElement = document.querySelector("#script_version_code").value; //Store the CodeTextElement to a variable
  62. } //Finishes the if condition
  63.  
  64. if (document.querySelector("input.Beautify").checked && SourceEditorCheck) { //Check if the Beautify checkbox is being checked and the syntax-highlighting source editor checkbox isn't checked
  65. CodeBackup = CodeTextElement.split('==/UserScript==')[1]; //Backup the actual script codes
  66. UserScriptBackup = CodeTextElement.split('==/UserScript==')[0] + '==/UserScript=='; //Backup the actual UserScript codes
  67. var FinalResponse = js_beautify(CodeTextElement.split('==/UserScript==')[1], JS_Beautifier_Options); //Add the beautified codes to a variable
  68. document.querySelector("li.current").innerText !== "Code" ? document.querySelector("#script_version_code").value = UserScriptBackup + '\n\n' + FinalResponse : document.querySelector("ol.linenums").innerText = UserScriptBackup + '\n\n' + FinalResponse; //Replaces the UnBeatified codes with the Beautified Codes
  69. } else { //Starts the else condition
  70. document.querySelector("li.current").innerText !== "Code" ? document.querySelector("#script_version_code").value = UserScriptBackup + CodeBackup : document.querySelector("ol.linenums").innerText = UserScriptBackup + CodeBackup; //If the checkbox is being uncheked, return the old UnBeautified Codes
  71. } //Finishes the else condition
  72. }; //Finishes the onlick listener
  73. //******************************************************************************************************************************************************************
  74. if (location.href.match(/^https:\/\/greasyfork\.org\/(?:[^\/]+\/)scripts\/(?:[^\/]+\/)code/)) //If the user is reading a code page
  75. { //Starts the if condition
  76. setTimeout(function() { //Starts the setTimeout function
  77. if (document.querySelector("li.current").innerText === "Code") { //Run only on the Code page
  78. var Lines = document.querySelectorAll("li"); //Create a variable to hold the total Code Lines
  79. for (var i = Lines.length; i--;) { //Starts the for condition
  80. Lines[i].style.background = 'none'; //Remove the grey line background
  81. } //Finishes the for condition
  82. } //Finishes the if condition
  83. }, 500); //Finishes the setTimeout function
  84. } //Finishes the if condition
  85. })();