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.

Fra 06.02.2021. Se den seneste versjonen.

  1. // ==UserScript==
  2. // @name Better GreasyFork Code Reader + JS Beautifier
  3. // @namespace BetterGreasyCodeReader
  4. // @version 0.5
  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://greatest.deepsurf.us/*/script_versions/new
  8. // @include https://greatest.deepsurf.us/*/scripts/*/versions/new
  9. // @icon https://www.google.com/s2/favicons?domain=greatest.deepsurf.us
  10. // @exclude https://greatest.deepsurf.us/*/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, UserScriptBackup, CodeTextElement, SourceEditorCheck; //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. document.querySelector("input.Beautify").onclick = function() { //When the checkbox is clicked
  65. if (LiCurrentISCode) { //Run only on the Code page
  66. CodeTextElement = document.querySelector("ol.linenums").innerText; //Store the CodeTextElement to a variable
  67. SourceEditorCheck = true; //Define the SourceEditorCheck variable as true
  68. } //Finishes the if condition
  69.  
  70. if (location.href.match('versions/new') !== null) { //Run only on the Post new script page
  71.  
  72. document.querySelector("#enable-source-editor-code").onclick = function() { //When the checkbox is clicked
  73. if (document.querySelector("#enable-source-editor-code").checked === true) { //If the SourceEditor is enabled
  74. document.querySelector("input.Beautify").disabled = true; //Disable the Beautifier button
  75. } //Finishes the if condition
  76. else { //Starts the else condition
  77. document.querySelector("input.Beautify").disabled = false; //Enable the Beautifier button
  78. } //Finishes the else condition
  79. }; //Finishes the onlick listener
  80.  
  81. SourceEditorCheck = document.querySelector("#enable-source-editor-code").checked === false; //Define the SourceEditorCheck variable as false
  82. CodeTextElement = document.querySelector("#script_version_code").value; //Store the CodeTextElement to a variable
  83. } //Finishes the if condition
  84.  
  85. 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
  86. CodeBackup = CodeTextElement.split('==/UserScript==')[1]; //Backup the actual script codes
  87. UserScriptBackup = CodeTextElement.split('==/UserScript==')[0] + '==/UserScript=='; //Backup the actual UserScript codes
  88. var FinalResponse = js_beautify(CodeTextElement.split('==/UserScript==')[1], JS_Beautifier_Options); //Add the beautified codes to a variable
  89. LiCurrentISCode !== true ? document.querySelector("#script_version_code").value = UserScriptBackup + '\n\n' + FinalResponse : document.querySelector("ol.linenums").innerText = UserScriptBackup + '\n\n' + FinalResponse; //Replaces the UnBeautified codes with the Beautified Codes
  90. } else { //Starts the else condition
  91. LiCurrentISCode !== true ? 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
  92. } //Finishes the else condition
  93. }; //Finishes the onlick listener
  94. })();