Skip to content

Instantly share code, notes, and snippets.

@quietlynn
Created February 25, 2012 15:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quietlynn/1908942 to your computer and use it in GitHub Desktop.
Save quietlynn/1908942 to your computer and use it in GitHub Desktop.
Google+ One Dimension
/*
Google+ One Dimension => Decode octet encoding in G+.
Copyright (C) 2012 Jingqin Lynn
Includes jQuery
Copyright 2011, John Resig
Dual licensed under the MIT or GPL Version 2 licenses.
http://jquery.org/license
Includes Sizzle.js
http://sizzlejs.com/
Copyright 2011, The Dojo Foundation
Released under the MIT, BSD, and GPL Licenses.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// ==UserScript==
// @name Google+ One Dimension
// @namespace http://project.quietmusic.org/j/
// @description Decode octet encoding in G+.
// @match https://plus.google.com/*
// ==/UserScript==
(function (name, url, main) {
'use strict';
//Use the <base> element to detect Google+ main page.
var base = document.querySelector('base');
if (!base || !base.href.match(/^https:\/\/plus\.google\.com(\/u\/\d+)?\/?/)) return;
var win = window;
if (typeof(unsafeWindow) != 'undefined') {
//Chrome V8 don't support unsafeWindow. Time for a hack.
if (window == unsafeWindow) {
var span = document.createElement('span');
span.setAttribute('onclick', 'return window;');
unsafeWindow = span.onclick();
}
win = unsafeWindow;
}
if (win.DependencyLoader.dependencyState(name) == win.DependencyLoader.Dependency.LOADING) {
main();
} else {
var dependencies = [
{ 'jQuery.gplus' : 'https://gist.github.com/raw/2645666/jquery.gplus.js' }
];
var me = {}; me[name] = url;
dependencies.push(me);
win.DependencyLoader.requireOrdered(dependencies,
null, function () {
win.DependencyLoader.require(name, main);
}
);
}
})(
'org.quietmusic.project.gplus.oneDimension',
'https://gist.github.com/raw/1908942/gplus.one_dimension.user.js',
function () {
'use strict';
var $ = window.jQuery;
var UTF8 = {
encode: function (str) {
return unescape(encodeURIComponent(str));
},
decode: function (str) {
return decodeURIComponent(escape(str));
}
};
var zeroCharCode = '0'.charCodeAt(0);
var aCharCodeMinus10 = 'a'.charCodeAt(0) - 10;
var defaultDigits = '0123456789abcdefghijklmnopqrstuvwxyz';
var maxCharCode = 255;
var octetEncode = function (str, digits) {
digits = digits || defaultDigits;
var digitsMap = {};
for (var i = 0; i < digits.length; i++) {
digitsMap[(i < 10 ? zeroCharCode : aCharCodeMinus10) + i] = digits.charAt(i);
}
var maxLength = maxCharCode.toString(digits.length).length;
str = UTF8.encode(str);
var sb = [];
for (var i = 0; i < str.length; i++) {
var oct = str.charCodeAt(i).toString(digits.length);
for (var j = 0; j < maxLength - oct.length; j++) {
sb.push(digits.charAt(0));
}
for (var j = 0; j < oct.length; j++) {
sb.push(digitsMap[oct.charCodeAt(j)]);
}
}
return sb.join('');
};
var octetDecode = function (str, digits) {
digits = digits || defaultDigits;
var digitsMap = {};
for (var i = 0; i < digits.length; i++) {
digitsMap[digits.charCodeAt(i)] = i;
}
var maxLength = maxCharCode.toString(digits.length).length;
var sb = [];
for (var i = 0; i < str.length; i += maxLength) {
var charCode = 0;
for(var j = 0; j < maxLength; j++) {
charCode = charCode * digits.length + digitsMap[str.charCodeAt(i + j)];
}
sb.push(charCode);
}
return UTF8.decode(String.fromCharCode.apply(String, sb));
};
defaultDigits = '—-─━┄┅┈┉';
$.gplus.page().dynamicSelect('content', function (content) {
content.xpath('.//text()', 'snapshot').each(function (_, text) {
text.data = text.data.replace(/[—\-─━┄┅┈┉]{3,}/g, function (match) {
try {
return '<!-- ' + octetDecode(match) + ' -->';
} catch(ex) {
return match;
}
});
});
}, 'snapshot');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment