Skip to content

Instantly share code, notes, and snippets.

@akuhn
Last active December 23, 2015 14:59
Show Gist options
  • Save akuhn/6652418 to your computer and use it in GitHub Desktop.
Save akuhn/6652418 to your computer and use it in GitHub Desktop.
Reverse web for Niko’s inverse party.
// ==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)
}
@nes1983
Copy link

nes1983 commented Sep 22, 2013

Missing semicolon in line 8 :)

@akuhn
Copy link
Author

akuhn commented Sep 23, 2013

Fixed…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment