Skip to content

Instantly share code, notes, and snippets.

@noromanba
Last active December 29, 2016 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noromanba/6f98b144447aa466684bdba871692360 to your computer and use it in GitHub Desktop.
Save noromanba/6f98b144447aa466684bdba871692360 to your computer and use it in GitHub Desktop.
escape URL for filesystem-failsafe for UserScript/Bookmarklet
// ==UserScript==
// @name filesafe
// @namespace http://noromanba.flavors.me
// @description escape URL for filesystem failsafe for UserScript/Bookmarklet
// @include http://example.com/DIY
// @grant none
// @noframes
// @run-at document-end
// @version 2016.12.29.0
// @homepage https://gist.github.com/noromanba/6f98b144447aa466684bdba871692360
// @downloadURL https://gist.github.com/noromanba/6f98b144447aa466684bdba871692360/raw/filesafe.user.js
// @license MIT License https://nrm.mit-license.org/2016
// @author noromanba http://noromanba.flavors.me
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Prometni_znak_C66.svg/128px-Prometni_znak_C66.svg.png
// ==/UserScript==
// Icon (PD by Rl91)
// https://commons.wikimedia.org/wiki/File%3APrometni_znak_C66.svg
// Devel
// https://gist.github.com/noromanba/6f98b144447aa466684bdba871692360
//*/
(() => {
'use strict';
const ensure = (url) => {
const insecure = /[\[\]()!:"'~]/g;
return url.pathname.replace(insecure, () => escape('$'));
};
// TODO replaceState keep title in browser history
//history.replaceState(null, null, paths.join('/'));
history.pushState(null, null, ensure(location));
// TODO handle inserted nodes after DOMContentLoaded
//*/
Array.from(document.links, link => {
link.pathname = ensure(link);
});
/*/
// FIXME unworks
new MutationObserver(records => {
records.forEach(record => {
let node = record.target;
if (!node.querySelectorAll) return;
if (node.tagName.toLowerCase() === 'a') {
node = node.parentNode;
}
Array.from(node.querySelectorAll('a[href]', link => {
if (!link.pathname) return;
link.pathname = ensure(link);
}));
});
}).observe(document.body, { childList: true, subtree: true });
//*/
})();
/*/
// Bookmarklet
javascript:(()=>{const ensure=(url)=>{const insecure=/[\[\]()!:"'~]/g;return url.pathname.replace(insecure,()=>escape('$'));};history.pushState(null,null,ensure(location));Array.from(document.links,link=>{link.pathname=ensure(link);});})();
//*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment