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-02-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Better GreasyFork Code Reader + JS Beautifier
  3. // @namespace BetterGreasyCodeReader
  4. // @version 0.7
  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. var LiCurrentISCode; //Makes the variable global
  41.  
  42. //******************************************************************************************************************************************************************
  43. if (location.href.match(/^https:\/\/greasyfork\.org\/(?:[^\/]+\/)scripts\/(?:[^\/]+\/)code/)) //If the user is reading a code page
  44. { //Starts the if condition
  45. LiCurrentISCode = true; //Set the variable as true
  46. setTimeout(function() { //Starts the setTimeout function
  47. if (LiCurrentISCode) { //Run only on the Code page
  48. var Lines = document.querySelectorAll("pre.linenums.prettyprinted li"); //Create a variable to hold the total Code Lines
  49. for (var i = Lines.length; i--;) { //Starts the for condition
  50. 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
  51. } //Finishes the for condition
  52. } //Finishes the if condition
  53. }, 500); //Finishes the setTimeout function
  54. } //Finishes the if condition
  55. //******************************************************************************************************************************************************************
  56.  
  57. 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
  58. var CodeBackup, CodeTextElement, SourceEditorCheck, IsNewScriptPage; //Makes these variables global
  59.  
  60. if (LiCurrentISCode !== true) { //If the li element doesn't exist
  61. LiCurrentISCode = false; //Set the variable as false
  62. } //Finishes the if condition
  63.  
  64. if (location.href.match('versions/new') !== null) { //Run only on the Post new script page
  65.  
  66. document.querySelector("#enable-source-editor-code").onclick = function() { //When the checkbox is clicked
  67. if (document.querySelector("#enable-source-editor-code").checked === true) { //If the SourceEditor is enabled
  68. document.querySelector("input.Beautify").disabled = true; //Disable the Beautifier button
  69. } //Finishes the if condition
  70. else { //Starts the else condition
  71. document.querySelector("input.Beautify").disabled = false; //Enable the Beautifier button
  72. } //Finishes the else condition
  73. }; //Finishes the onlick listener
  74.  
  75. SourceEditorCheck = document.querySelector("#enable-source-editor-code").checked === false; //Define the SourceEditorCheck variable as false
  76. CodeTextElement = document.querySelector("#script_version_code").value; //Store the CodeTextElement to a variable
  77. IsNewScriptPage = true; //Set the variable as true
  78. } //Finishes the if condition
  79.  
  80. document.querySelector("input.Beautify").onclick = function() { //When the checkbox is clicked
  81. if (LiCurrentISCode) { //Run only on the Code page
  82. CodeTextElement = document.querySelector("ol.linenums").innerText; //Store the CodeTextElement to a variable
  83. SourceEditorCheck = true; //Define the SourceEditorCheck variable as true
  84. } //Finishes the if condition
  85.  
  86. if (IsNewScriptPage) { //Run only on the Post new script page
  87. if (document.querySelector("input.Beautify").checked === true) { //If the Beautifier is enabled
  88. document.querySelector("#enable-source-editor-code").disabled = true; //Disable the SourceEditor button
  89. } //Finishes the if condition
  90. else { //Starts the else condition
  91. document.querySelector("#enable-source-editor-code").disabled = false; //Enable the SourceEditor button
  92. } //Finishes the else condition
  93.  
  94. SourceEditorCheck = document.querySelector("#enable-source-editor-code").checked === false; //Define the SourceEditorCheck variable as false
  95. CodeTextElement = document.querySelector("#script_version_code").value; //Store the CodeTextElement to a variable
  96. } //Finishes the if condition
  97.  
  98. 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
  99. CodeBackup = CodeTextElement; //Backup the actual script codes
  100. var BeautifiedCodes = js_beautify(CodeTextElement, JS_Beautifier_Options); //Add the beautified codes to a variable
  101. LiCurrentISCode !== true ? document.querySelector("#script_version_code").value = BeautifiedCodes : document.querySelector("ol.linenums").innerText = BeautifiedCodes; //Replaces the UnBeautified codes with the Beautified Codes
  102. } else { //Starts the else condition
  103. 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
  104. } //Finishes the else condition
  105. }; //Finishes the onlick listener
  106. })();