Skip to content

Instantly share code, notes, and snippets.

@nes1983
Forked from akuhn/reverse.user.js
Last active December 23, 2015 15:59
Show Gist options
  • Save nes1983/6659301 to your computer and use it in GitHub Desktop.
Save nes1983/6659301 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Reverse Web
// @namespace http://www.example.com
// @grant
// ==/UserScript==
function reverseWordButKeepCase(str) {
var buffer = "";
for (var i = 0, j = str.length - 1; i < str.length; i++, j--) {
if (str.charAt(i) == str.charAt(i).toUpperCase()) {
buffer += str.charAt(j).toUpperCase();
}
else {
buffer += str.charAt(j).toLowerCase();
}
}
return buffer;
}
function reverseText(str) {
return str.replace(/\w+/g,reverseWordButKeepCase)
}
var path = "//text()[not(ancestor::script or ancestor::style)]"
var arr = document.evaluate(path, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < arr.snapshotLength; i++) {
var each = arr.snapshotItem(i)
each.textContent = reverseText(each.textContent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment