quarxpl / RKLinks

// ==UserScript==
// @name		RKLinks
// @description		Odnośniki na blogu "Powrót Roberta"
// @match		https://disqus.com/embed/comments/*
// @version		1.0
// ==/UserScript==

if(window.top == window.self) return;
if(window.location.search.indexOf('powrotroberta.blogspot.com') == -1)return;

var MutationObserver	= window.MutationObserver;
var observer		= new MutationObserver (update);
var config = {
	attributes: true,
	characterData: true,
	childList: true,
	subtree: true,
	attributeOldValue: true,
	characterDataOldValue: true
};

function inspect(node) {
	if (node.nodeType !== 1) return;
	if (!node.classList.contains ("post-content"))return;
	var body = node.querySelector("div.post-message div");
	if (body == null) return;
	var regex = /(http:\/\/|https:\/\/|www\(\.\)|www\(\,\))[^ <"]+/g
	var changed = false;
	var content = body.innerHTML;

	content = content.replace(regex, function(url) {
		var nurl = url.replace(/\(\.\)/g, ".").replace(/,/g,".");
		if (nurl !== url) {
			changed = true;
			if (!nurl.startsWith("http"))
				nurl = "http://" + nurl;
			return "<a href = \"" + nurl + "\" target = \"_blank\">" + nurl + "</a>";
		}
		return url;
	});

	if (changed) {
		body.innerHTML = content;
	}
}

function update(records) {

	records.forEach(function(mutation) {

		if (mutation.type == "childList" && typeof mutation.addedNodes  == "object" && mutation.addedNodes.length ) {
			for(var i = 0, L = mutation.addedNodes.length; i < L; ++i) {
				inspect(mutation.addedNodes[i]);
			}	
		}else if (mutation.type == "attributes") {
			inspect(mutation.target);
		}

	});
}

var init = function() {

	var root = document.getElementById('conversation');
	
	if(root) {
		observer.observe(root, config);
	} else {
		setTimeout(init, 100);
	}

};

init();