Created
January 30, 2014 10:08
-
-
Save RubaXa/8705665 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name lepratranslate | |
// @author RubaXa <trash@rubaxa.org> | |
// @license MIT | |
// @version 0.0.1 | |
// @include http://leprosorium.ru/* | |
// ==/UserScript== | |
/*global unsafeWindow*/ | |
(function (window){ | |
'use strict'; | |
(function (factory){ | |
// Export | |
var script = document.createElement('script'); | |
script.textContent = '(' + factory + ')();'; | |
window.document.body.appendChild(script); | |
})(function (){ | |
'use strict'; | |
var TEXTOMATE_URL = '/fraud/textomate/'; | |
var MONTHS_MAP = { | |
'Января': 1, | |
'Февраля': 2, | |
'Марта': 3, | |
'Апреля': 4 | |
}; | |
var words = []; // Список замен | |
function load(url, callback){ | |
var xhr = new XMLHttpRequest; | |
xhr.open('GET', url, true); | |
xhr.onload = function (){ | |
callback(xhr.responseText); | |
}; | |
xhr.send(); | |
} | |
function matchAll(str, regexp){ | |
var result = [], match; | |
while( match = regexp.exec(str) ){ | |
result.push(match); | |
} | |
return result; | |
} | |
function toRegExp(str, word){ | |
return new RegExp(str.replace(/([()[{*+.$^\\|?])/g, '\\$1'), 'g'); | |
} | |
function parseDate(day, month, year, time){ | |
return new Date(year+'-'+MONTHS_MAP[month]+'-'+day+' '+time); | |
} | |
function parseWords(html){ | |
var word_to = matchAll(html, /"fow_to_word">(.*?)</g); | |
var expires = matchAll(html, /(\d+) ([а-я]+) (\d+), (\d+:\d+)/gi); | |
matchAll(html, /"fow_word">(.*?)</g).forEach(function (match, i){ | |
var start = new Date; | |
var expire = parseDate(expires[i][1], expires[i][2], expires[i][3], expires[i][4]); | |
start.setTime(expire.getTime()-24*60*60*1000); | |
words.push({ | |
search: match[1] | |
, regexp: toRegExp(word_to[i][1], match[1]) | |
, replace: word_to[i][1] | |
, start: start.getTime() | |
, expire: expire.getTime() | |
}); | |
}); | |
} | |
function loadWords(callback){ | |
load(TEXTOMATE_URL, function (html){ | |
parseWords(html); | |
var matches = matchAll(html, /href="(\/*.?\/pages\/\d+)"/g); | |
(function _parseNextPage(){ | |
var match = matches.shift(); | |
if( match ){ | |
load(match[1], function (html){ | |
parseWords(html); | |
_parseNextPage(); | |
}); | |
} else { | |
callback(); | |
} | |
})(); | |
}); | |
} | |
function translate(id, className){ | |
var el = document.getElementById(id); | |
console.log('translate: ', id, className); | |
if( el ){ | |
var items = el.getElementsByClassName(className), i = items.length; | |
while( i-- ){ | |
var date = items[i].getElementsByClassName('p')[0].innerHTML.match(/(\d+)\.(\d+)\.(\d+)\s+в\s(\d+)\.(\d+)/) | |
date = (date ? new Date(date[3]+'-'+date[2]+'-'+date[1]+' '+date[4]+':'+date[5]) : new Date).getTime(); | |
translateNodes(items[i], date); | |
} | |
} | |
} | |
function translateNodes(el, date){ | |
var | |
childNodes = el.childNodes | |
, i = childNodes.length | |
; | |
while( i-- ){ | |
el = childNodes[i]; | |
if( el.nodeType == el.TEXT_NODE ){ | |
var value = el.nodeValue; | |
words.forEach(function (word){ | |
if( date > word.start && date < word.expire ){ | |
value = value.replace(word.regexp, word.search); | |
} | |
}); | |
if( el.nodeValue !== value ){ | |
console.log(el.nodeValue.trim(), '\n', value.trim(), '\n~~~~~~'); | |
el.nodeValue = value; | |
} | |
} | |
else if( el.childNodes ){ | |
translateNodes(el, date); | |
} | |
} | |
} | |
// Load all words | |
loadWords(function (){ | |
console.log('words loaded:', words); | |
translate('js-posts_holder', 'post'); | |
translate('js-commentsHolder', 'post'); | |
}); | |
}); | |
})(typeof unsafeWindow === 'undefined' ? window : unsafeWindow); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment