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-03-27 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Better GreasyFork Code Reader + JS Beautifier
  3. // @namespace BetterGreasyCodeReader
  4. // @version 0.8
  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. // ==/UserScript==
  16. (function() {
  17. 'use strict';
  18.  
  19. var JS_Beautifier_Options = { //Beginning of the "Your Selected Options (JSON):" Content
  20. "indent_size": "2",
  21. "indent_char": " ",
  22. "max_preserve_newlines": "5",
  23. "preserve_newlines": true,
  24. "keep_array_indentation": false,
  25. "break_chained_methods": false,
  26. "indent_scripts": "normal",
  27. "brace_style": "collapse",
  28. "space_before_conditional": true,
  29. "unescape_strings": false,
  30. "jslint_happy": false,
  31. "end_with_newline": false,
  32. "wrap_line_length": "0",
  33. "indent_inner_html": false,
  34. "comma_first": false,
  35. "e4x": false,
  36. "indent_empty_lines": false
  37. }; //End of the "Your Selected Options (JSON):" Content
  38.  
  39. var LiCurrentISCode; //Makes the variable global
  40.  
  41. //******************************************************************************************************************************************************************
  42. if (location.href.match(/^https:\/\/greasyfork\.org\/(?:[^\/]+\/)scripts\/(?:[^\/]+\/)code/)) //If the user is reading a code page
  43. { //Starts the if condition
  44. LiCurrentISCode = true; //Set the variable as true
  45. setTimeout(function() { //Starts the setTimeout function
  46. if (LiCurrentISCode) { //Run only on the Code page
  47. var Lines = document.querySelectorAll("pre.linenums.prettyprinted li"); //Create a variable to hold the total Code Lines
  48. for (var i = Lines.length; i--;) { //Starts the for condition
  49. Lines[i].setAttribute("style", "background: none;box-shadow: -1px 1px 2px rgba(255, 211, 0, 0.2);"); //Remove the grey line background and add a zebbra line effect
  50. } //Finishes the for condition
  51. } //Finishes the if condition
  52. }, 500); //Finishes the setTimeout function
  53. } //Finishes the if condition
  54. //******************************************************************************************************************************************************************
  55.  
  56. 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
  57. var CodeBackup, CodeTextElement, SourceEditorCheck, IsNewScriptPage; //Makes these variables global
  58.  
  59. if (LiCurrentISCode !== true) { //If the li element doesn't exist
  60. LiCurrentISCode = false; //Set the variable as false
  61. } //Finishes the if condition
  62.  
  63. if (location.href.match('versions/new') !== null) { //Run only on the Post new script page
  64.  
  65. document.querySelector("#enable-source-editor-code").onclick = function() { //When the checkbox is clicked
  66. if (document.querySelector("#enable-source-editor-code").checked === true) { //If the SourceEditor is enabled
  67. document.querySelector("input.Beautify").disabled = true; //Disable the Beautifier button
  68. } //Finishes the if condition
  69. else { //Starts the else condition
  70. document.querySelector("input.Beautify").disabled = false; //Enable the Beautifier button
  71. } //Finishes the else condition
  72. }; //Finishes the onlick listener
  73.  
  74. SourceEditorCheck = document.querySelector("#enable-source-editor-code").checked === false; //Define the SourceEditorCheck variable as false
  75. CodeTextElement = document.querySelector("#script_version_code").value; //Store the CodeTextElement to a variable
  76. IsNewScriptPage = true; //Set the variable as true
  77. } //Finishes the if condition
  78.  
  79. document.querySelector("input.Beautify").onclick = function() { //When the checkbox is clicked
  80. if (LiCurrentISCode) { //Run only on the Code page
  81. CodeTextElement = document.querySelector("ol.linenums").innerText; //Store the CodeTextElement to a variable
  82. SourceEditorCheck = true; //Define the SourceEditorCheck variable as true
  83. } //Finishes the if condition
  84.  
  85. if (IsNewScriptPage) { //Run only on the Post new script page
  86. SourceEditorCheck = document.querySelector("#enable-source-editor-code").checked === false; //Define the SourceEditorCheck variable as false
  87. CodeTextElement = document.querySelector("#script_version_code").value; //Store the CodeTextElement to a variable
  88. } //Finishes the if condition
  89.  
  90. 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
  91. CodeBackup = CodeTextElement; //Backup the actual script codes
  92. var BeautifiedCodes = js_beautify(CodeTextElement, JS_Beautifier_Options); //Add the beautified codes to a variable
  93. LiCurrentISCode !== true ? document.querySelector("#script_version_code").value = BeautifiedCodes : document.querySelector("ol.linenums").innerText = BeautifiedCodes; //Replaces the UnBeautified codes with the Beautified Codes
  94. } else { //Starts the else condition
  95. LiCurrentISCode !== true ? document.querySelector("#script_version_code").value = CodeBackup : document.querySelector("ol.linenums").innerText = CodeBackup; //If the checkbox is being uncheked, return the old UnBeautified Codes
  96. } //Finishes the else condition
  97. }; //Finishes the onlick listener
  98. })();