// ==UserScript==
// @name Supercharged Local Directory File Browser
// @version 2.7.1
// @description Makes file:/// directory ("Index of...") pages (and many server-generated index pages) actually useful. Adds navigation links, file preview pane, keyboard navigation, user-defined shortcuts, filtering, more.
// @author Gaspar Schott (Michael Schrauzer) mshroud@gmail.com
// @license GPL-3.0-or-later
// @homepageURL https://openuserjs.org/scripts/gaspar_schot/Supercharged_Local_Directory_File_Browser
// @contributionURL https://paypal.me/mschrauzer
// @include file://*/
// @include file://*/?*
// @require http://code.jquery.com/jquery-latest.min.js
// NOTE: This script was developed in Vivaldi, running on Mac OS High Sierra. It has been tested in various Chrome and Gecko-based browsers.
// It has been minimally tested on Windows and not at all on other OSes. It should work, but please report any issues.
// The script does not work in Safari because Safari does not allow local directories to be browsed.
// NOTE: By default, Greasemonkey and Tampermonkey will not run scripts on file:/// urls, so for this script to work, you will have to enable it first.
// For Tampermonkey, go to Chrome extension page, and tick the 'Allow access to file URLs' checkbox at the Tampermonkey extension section.
// For Greasemonkey, open about:config and change greasemonkey.fileIsGreaseable to true.
// @namespace https://greasyfork.org/users/16170
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
var $ = jQuery;
// ***** USER SETTINGS ***** //
const $settings = {
// Paste your exported settings between the two lines below:
//--------------------------------------------------------//
shortcuts: // N.B.: Directory links must end with "/", file links must not.
// You may add as many menus and links as you like; just copy the example below and edit as needed.
// Local directory shortcuts must begin with "file:///"; external shortcuts must begin with the correct protocol ("http://" or "ftp://", etc.).
// Note that because of same-origin security concerns, the browser will not allow you to navigate from an external webpage to a local directory.
// But see this page for possible workarounds: https://stackoverflow.com/questions/39007243/cannot-open-local-file-chrome-not-allowed-to-load-local-resource
[
{
"menu_title":"My Sample Menu",
"links":
[
{ "link_name":"My Directory Link 1", "link":"file:///Path/To/My/Directory/" },
{ "link_name":"My Directory Link 2", "link":"file:///Path/To/My/Directory_2/" },
{ "link_name":"My External Link", "link":"https://www.mywebpage.com/" },
{ "link_name":"My File Link", "link":"file:///Path/To/My/File.ext" },
]
},
],
UI_font: // Choose an installed font for the UI.
'lucidagrande',
UI_font_size: // Choose a default UI font size; use any standard CSS units.
'13px',
alternate_background: // If true (default false), alternate sidebar row background color.
false,
ignore_files: // If true (default), ignored files (see below) will be greyed-out (default) in the file list and will not be loaded in the content pane when selected;
// If false, they will be treated as normal files, so if they are selected, the browser will attempt to download any file types it can't handle (which makes keyboard navigation inconvenient).
true,
hide_ignored_files: // If true, ignored files will be completely hidden from the file list;
// If false (default), ignored files will appear greyed-out.
false,
hide_invisibles: // Un*x/Mac OS only: If true (default), files or directories beginning with a "." will be hidden.
true,
hide_details: // If true (default), hide file and directory details; if false, show them.
true,
default_sort: // Choose from: 'name', 'size', 'date', 'kind', 'ext', 'default'.
// default = Chrome sorting: dirs on top, files alphabetical.
'default',
dirs_on_top: // If true, directories will always be listed firs except when sorting by "name" (since otherwise sorting by "name" would equal "default").
// If false (default), directories and files will be sorted together. (In practice, dirs will typically still be separated when sorting by size, kind, and extension.)
false,
apps_as_dirs: // Un*x/Mac OS only: if true, treat apps as directories; allows app contents to be browsed. This is the default behavior for Chrome.
// If false (default), treat apps as ignored files.
false,
dark_theme: // If true (default: false), gives the content pane a dark background, and inverts html and text content.
false,
grid_image_size: // Default = 150
150,
grid_font_size: // Default = 1
1,
autoload_index_files: // If true (default: false), automatically select first "index.ext" file found in directory.
false,
autoload_audio: // If true, the first audio file found in a directory will be automatically selected and loaded for playback.
// If true, use left/right arrows to select audio files, use up/down arrows to select non-audio files (including video) only.
// If false (default), audio files will be treated like any other file for navigation purposes (i.e., up and down arrows will select them too).
// Note: if false, using up/down arrows will automatically begin playback of selected audio files; left/right arrows will select audio files without playing them.
false,
use_custom_icons: // if true (default), use custom icons for dirs and files
// if false, use browser/server default icons
true
//--------------------------------------------------------//
// Paste your exported settings between the above two lines.
};
// ***** END USER SETTINGS ***** //
// KEYBINDINGS:
// Arrow Up/Down: Navigate up and down directory items
// Cmd/Ctrl + Arrow Up: Go to parent directory
// Arrow Left/Right: Navigate to prev/next image, font, or audio file, skipping other files, when files of these types are selected.
// Opt/Alt + Arrow Left/Right: Skip audio ±10s
// Shift + Opt/Alt + Arrow Left/Right: Skip audio ±30s
// Arrow Right: Open selected directory.
// Space: Pause/Play audio
// Return: Open selected directory.
// Cmd/Ctrl + D: Toggle file details (size, date modified) in some index page types.
// Cmd/Ctrl + G: Show or Reset Grids
// Cmd/Ctrl + I: Toggle Invisibles
// Cmd/Ctrl + Shift + O: Open selected item in new window/tab
// Cmd/Ctrl + R: Reload previewed content, reset scaled images/fonts.
// Cmd/Ctrl + W: Close previewed content (not available in Firefox), or close window if no content is being previewed.
// Cmd/Ctrl + Shift + >: Scale preview items and grids.
// CHANGELOG HISTORY:
//• 2.7.1
// Added: User settings to choose default UI font-family and UI font-size.
// Added: User setting and menu item to alternate sidebar row background color.
// Improved: Directory preview styling: alternating row background, font-family and font-size honor user UI settings.
// Internal: Completely reorganized CSS by properties instead of selectors. There may be lingering glitches; let me know if you find any.
//• 2.7.0: New features and user settings. Please export your user_settings before updating and note the changes mentioned below.
//• NEW (and experimental): Display a standard index of the selected sidebar directory in the preview pane.
// This is very handy for browsing directories from the sidebar.
// Clicking files and directories directly in the preview pane works as you'd expect, but it can be a little weird,
// since you can navigate up and down the directory structure in the preview pane independently from the sidebar.
//• NEW: Added URL query strings to remember directory selection history and various temporary UI settings between directory changes:
// sort order, show/hide details, default/dark mode, sidebar width, and previously selected directory.
//• NEW: Select files and directories by typed string.
//• Improved: Converted user shortcut settings to JSON for greater flexibility.
// Now you can create and name your own shortcut menu categories.
// IMPORTANT: You must re-enter your old shortcuts in the new settings format.
//• Added: Menu item to reset/delete query strings and return to the default user_settings in the script.
//• Added: New user setting to show/hide details by default. (Don't know why it took so long to add this.)
//• Added: User customizable file kinds for sorting.
// You may add or remove file kind categories and file extensions to suit your own files:
// See "$row_types" in code below, at the beginning of "General Setup" and just after the Changelog, to add your own.
//• Removed "ignore_file_types" from the user_settings, because the list of ignored file extensions has been moved:
// See "$row_settings" in code below, at the beginning of "General Setup" and just after the Changelog, to add your own.
//• Removed "user_name" from the user_settings, because it is no longer needed.
//• Improved: File shortcuts work much more reliably now, and thanks to query strings, you can now bookmark more than one file in a directory.
//• Improved: Various small UI changes.
//• Fixed: Toggle sidebar wasn't working.
//• Fixed: Ignored files weren't being sorted.
//• Internals: Abstracted a bunch of functions, removed some unneeded variables and functions. More to be done.
//• If you like this script, please consider making a donation. See the "Donate" menu item. Thanks!
//• 2.6.5
//• Fixed: First and last audio files would repeat when ended instead of going to next track in some circumstances.
//• Fixed: Various issues with file navigation.
//• 2.6.4
//• Added: Nice custom icons for dirs and various file types. Other minor UI tweaks.
//• Added: New user setting to use default file icons instead the new custom ones, in case you don't like them.
//• Added: Separate close button for audio player, since there was no way to close it while leaving other previewed content open.
//• Fixed: Really, really fixed reload button and reload shortcut not working (I hope).
//• Fixed: Yet more issues with arrow navigation.
//• Fixed: Various UI issues with Firefox.
//• Improved: Sidebar resizing should be more reliable (drag event was interacting with sidebar).
//• Other: Abstracted some styles and cleaned up code.
//• Many other bug fixes.
//• 2.6.3: Minor UI improvements and bugfixes.
//• Changed: Open dirs on double-click. (You can still open them by selecting and pressing "Return" or the Right Arrow Key.)
//• Changed: Removed autoplay_audio preference.
//• Fixed: Reload button and reload shortcut weren't working correctly.
//• Fixed: Clicking audio file name didn't pause/play file.
//• Fixed: Sidebar height didn't reset when changing sort order.
//• Fixed: Directories with a "." in the name were being sorted as if the trailing text was an extension.
//• Fixed: Corrected some code that prevented the script from working in Safari; while Safari still doesn't allow local directories to be browsed, the script will work on server-generated pages.
//• Other: Removed some unneeded styles and functions, simplified arrow navigation code.
//• 2.6.2: Fixes for a few more pesky bugs. Just when you think you're safe.... Apologies.
//• Fixed: Reload and Close buttons and keyboard combinations now work for audio files.
//• Fixed: Preview pane titlebar wasn't showing in some circumstances.
//• 2.6.1: Hopefully fixed a bug when navigating folders containing audio files. Sorry about that!
//• 2.6.0: Sorting, new user settings, and much more! This is a pretty substantial update.
//• Added: Donate link (paypal.me/mschrauzer) to the main shortcuts menu. If you like this script, please support its development. Thank you!
//• Added: Support for additional common server-generated index types. The script should now work on index pages structured as unordered lists, "
" text, and html tables.
//• Added: Sorting by name, size, date modified, kind, file extension, and default Chrome sorting (i.e., dirs on top, files alphabetical).
//• Click the "Details" button (or Cmd/Ctrl + D) to show the available sort options.
//• Note: Some server configurations don't provide size or date information; on such sites, these sorting options will not be available.
//• Note: Sorting by kind is based on the file extension, so it is rather coarse (at least for now); many non-image/audio/pdf files will simply be categorized as generic "Files".
//• Added: Added "Sort by..." menu item in the main shortcuts menu.
//• Added: New User Settings (gives you control over some behavior added in the last update):
//• default_sort: Choose default sort method: by name, size, date, kind, extension, or Chrome default (dirs on top, files alphabetical).
//• dirs_on_top: Choose whether directories should be sorted separately from files.
//• autoload_index_files: If true (default false), autoload first index.[ext] file found in directory.
//• autoload_audio:
//• If false (default), treat audio files like other files for navigation purposes. Use up and down arrows as normal, only show audio controls when an audio file is selected.
//• (Note that when false, only one file in the directory at a time can be selected.)
//• If true, autoload first audio file in directory (and cover art, if any) and navigate audio with left and right arrows, other files with up and down arrows. This was the previous behavior of the script.
//• Added: Alert reminder to add your computer user account name to the settings.
//• Enhancement: UI: Added flyout submenus to main shortcuts menu.
//• Enhancement: Added .aiff, .ape, .m4a to recognized audio format types.
//• Enhancement: Added .svg to recognized image format types (they now are shown in an element instead of an