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.

Устаревшая версия за 04.02.2021. Перейдите к последней версии.

// ==UserScript==
// @name         Better GreasyFork Code Reader + JS Beautifier
// @namespace    BetterGreasyCodeReader
// @version      0.4
// @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.
// @author       hacker09
// @include      https://greasyfork.org/*/script_versions/new
// @include      https://greasyfork.org/*/scripts/*/versions/new
// @icon         https://www.google.com/s2/favicons?domain=greasyfork.org
// @exclude      https://greasyfork.org/*/script_versions/new?language=css
// @include      /^https:\/\/greasyfork\.org\/(?:[^\/]+\/)scripts\/(?:[^\/]+\/)code/
// @require      https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.4/beautify.js
// @run-at       document-end
// @grant        none
// @gnoiframes
// ==/UserScript==
(function() {
  'use strict';

  var JS_Beautifier_Options = { //Beginning of the "Your Selected Options (JSON):" Content
    "indent_size": "2",
    "indent_char": " ",
    "max_preserve_newlines": "5",
    "preserve_newlines": true,
    "keep_array_indentation": false,
    "break_chained_methods": false,
    "indent_scripts": "normal",
    "brace_style": "collapse",
    "space_before_conditional": true,
    "unescape_strings": false,
    "jslint_happy": false,
    "end_with_newline": false,
    "wrap_line_length": "0",
    "indent_inner_html": false,
    "comma_first": false,
    "e4x": false,
    "indent_empty_lines": false
  }; //End of the "Your Selected Options (JSON):" Content

  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
  var CodeBackup, UserScriptBackup, CodeTextElement, SourceEditorCheck;

  document.querySelector("input.Beautify").onclick = function() { //When the checkbox is clicked
    if (document.querySelector("li.current").innerText === "Code") { //Run only on the Code page
      CodeTextElement = document.querySelector("ol.linenums").innerText; //Store the CodeTextElement to a variable
      SourceEditorCheck = true; //Define the SourceEditorCheck variable as true
    } //Finishes the if condition

    if (location.href.match('versions/new') !== null) { //Run only on the Code page

      document.querySelector("#enable-source-editor-code").onclick = function() { //When the checkbox is clicked
        if (document.querySelector("#enable-source-editor-code").checked === true) { //If the SourceEditor is enabled
          document.querySelector("input.Beautify").disabled = true; //Disable the Beautifier button
        } //Finishes the if condition
        else { //Starts the else condition
          document.querySelector("input.Beautify").disabled = false; //Enable the Beautifier button
        } //Finishes the else condition
      }; //Finishes the onlick listener

      SourceEditorCheck = document.querySelector("#enable-source-editor-code").checked === false; //Define the SourceEditorCheck variable as false
      CodeTextElement = document.querySelector("#script_version_code").value; //Store the CodeTextElement to a variable
    } //Finishes the if condition

    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
      CodeBackup = CodeTextElement.split('==/UserScript==')[1]; //Backup the actual script codes
      UserScriptBackup = CodeTextElement.split('==/UserScript==')[0] + '==/UserScript=='; //Backup the actual UserScript codes
      var FinalResponse = js_beautify(CodeTextElement.split('==/UserScript==')[1], JS_Beautifier_Options); //Add the beautified codes to a variable
      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
    } else { //Starts the else condition
      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
    } //Finishes the else condition
  }; //Finishes the onlick listener
  //******************************************************************************************************************************************************************
  if (location.href.match(/^https:\/\/greasyfork\.org\/(?:[^\/]+\/)scripts\/(?:[^\/]+\/)code/)) //If the user is reading a code page
  { //Starts the if condition
    setTimeout(function() { //Starts the setTimeout function
      if (document.querySelector("li.current").innerText === "Code") { //Run only on the Code page
        var Lines = document.querySelectorAll("pre.linenums.prettyprinted li"); //Create a variable to hold the total Code Lines
        for (var i = Lines.length; i--;) { //Starts the for condition
          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
        } //Finishes the for condition
      } //Finishes the if condition
    }, 500); //Finishes the setTimeout function
  } //Finishes the if condition
})();