const typeCheck = (expectedType, param) => {
if (typeof expectedType !== "string") throw new TypeError(`expectedType parameter must be a string but got "${typeof expectedType}"`)
if (typeof param !== expectedType) throw new TypeError(`Parameter type must be a ${expectedType} but got "${typeof param}"`)
}
const normalizeTopicUrl = (href) => {
try {
const u = new URL(href, location.href);
const normHost = (h) => h.replace(/^www\./, "");
if (normHost(u.hostname) !== normHost(location.hostname)) return null;
u.search = "";
u.hash = "";
u.pathname = u.pathname
.replace(/\/unread\/?$/i, "")
.replace(/\/+$/, "");
return u.toString();
} catch {
return null;
}
};
const UserPreferences = () => {
const data = {
liked: new Set(),
disliked: new Set()
}
const addToLiked = (url) => { typeCheck("string", url)
const normalizedUrl = normalizeTopicUrl(url);
data.liked.add(normalizedUrl);
data.disliked.delete(normalizedUrl);
}
const addToDisliked = (url) => { typeCheck("string", url)
const normalizedUrl = normalizeTopicUrl(url);
data.disliked.add(normalizedUrl);
data.liked.delete(normalizedUrl);
}
const isLiked = (url) => { typeCheck("string", url)
const normalizedUrl = normalizeTopicUrl(url);
return data.liked.has(normalizedUrl)
}
const isDisliked = (url) => { typeCheck("string", url)
const normalizedUrl = normalizeTopicUrl(url);
return data.disliked.has(normalizedUrl)
}
return { addToDisliked, addToLiked, isLiked, isDisliked };
}
const LinkModifier = () => {
const isTopic = (url) => { typeCheck("string", url);
return url.includes("sosyal/konu");
}
const data = {
linkElements : new Set()
}
const findLinks = () => {
data.linkElements.clear();
const origin = "techolay.net";
const linkElements = document.querySelectorAll('a[href]');
for (const linkElement of linkElements) {
const href = linkElement.href;
if (href.includes(origin) && isTopic(href)) {
data.linkElements.add(linkElement);
}
}
};
const restyleLinks = (userPreferences) => {
for (const linkElement of data.linkElements) {
const href = normalizeTopicUrl(linkElement.href);
if (!href || !isTopic(href)) continue;
if (userPreferences.isLiked(href)) linkElement.style.color = "green";
else if (userPreferences.isDisliked(href)) linkElement.style.color = "red";
}
};
findLinks();
return { findLinks, restyleLinks };
}
const userPreferences = UserPreferences();
const linkModifier = LinkModifier();
// ornek kullanimi.
userPreferences.addToLiked("https://techolay.net/sosyal/konu/verilen-reaksiyona-gore-kutu-rengi-nasil-degistirilir.134155/")
linkModifier.restyleLinks(userPreferences);