Skip to content

Instantly share code, notes, and snippets.

@akinov
Last active March 7, 2018 08:37
Show Gist options
  • Save akinov/a38822c6aaec04e8f1d1ce9fffd932f7 to your computer and use it in GitHub Desktop.
Save akinov/a38822c6aaec04e8f1d1ce9fffd932f7 to your computer and use it in GitHub Desktop.
Toggle whitespace for Github PR
// ==UserScript==
// @name Toggle whitespace
// @namespace https://gist.github.com/akinov/a38822c6aaec04e8f1d1ce9fffd932f7
// @version 0.1
// @description Diff miyasui!!
// @author akinov
// @match https://github.com/*/pull/*
// @grant none
/* load jQuery */
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
(function() {
'use strict';
let query = convertQueryToObject();
let buttonText = 'Hide';
if ('w' in query) {
buttonText = 'Show';
}
//location.href = updateQueryStringParameter(location.href, 'w', 1);
const $toggeBtn = $(`<button>${buttonText} whitespace</button>`).addClass('btn btn-sm').css({
position: 'fixed',
right: '20px',
top: '120px'
}).on('click', toggleWQuery);
$('body').append($toggeBtn);
function toggleWQuery() {
if ('w' in query) {
delete query.w;
} else {
query.w = 1;
}
location.search = buildQueryString(query);
}
function convertQueryToObject() {
var obj = {},
query = location.search,
params = query.split(/[?&]/),
p, key, val;
for (var i = 0, len = params.length; i < len; ++i) {
p = params[i].split("=");
key = normalize(p[0]);
val = normalize(p[1]);
if (key) {
obj[key] = val;
}
}
return obj;
}
function buildQueryString(obj) {
var query = "",
key, val;
for (key in obj) {
if (!obj.hasOwnProperty(key)) {
continue;
}
val = obj[key];
query += (query === "") ? "" : "&";
query += fixedEncodeURIComponent(key) + "=";
if (val != null) {
query += fixedEncodeURIComponent(val);
}
}
return query;
}
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}
function fixedDecodeURIComponent(str) {
return decodeURIComponent(str.replace(/[!'()*]/g, unescape));
}
function normalize(arg) {
return (!arg) ? null : fixedDecodeURIComponent(arg);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment