<commit_before>var lastTitle = "";

function getInfo(titleNode, episodeNode, callback) {
	var info = {};
	if (titleNode) {
		info["title"] = titleNode.textContent;
	}
	if (episodeNode) {
		var text = episodeNode.textContent;
		var regex = /\D*(\d+)\D*(\d+)/
		var match = regex.exec(text);
		info["season"] = match[1];
		info["episode"] = match[2];
	}
	callback(info);
}

function getRatings(title, season, episode, callback) {
	if (title && title.length && (!lastTitle || lastTitle == title || !title.endsWith(lastTitle))) {
		lastTitle = title;
		fetchRatings(title, season, episode, function(ratings) {
			callback(ratings);
		});
	}
}

function getInfoAndRatings(titleNode, episodeNode, callback) {
	getInfo(titleNode, episodeNode, function(info) {
		getRatings(info["title"], info["season"], info["episode"], callback);
	})
}

MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

var observerOptions = {
	childList: true,
	subtree: true
}

var jawBoneContentObserver = new MutationObserver(function(mutations, observer) {
	var node = mutations[mutations.length - 1].target;
	getInfoAndRatings(node.querySelector(".jawBone > h3"), null, function(ratings) {
		injectRatings(node.querySelector(".meta"), ratings);
	});
});

var titleCardObserver = new MutationObserver(function(mutations, observer) {
	var node = mutations[mutations.length - 1].target;
	getInfoAndRatings(node.querySelector(".bob-title"), null, function(ratings) {
		injectRatings(node.querySelector(".meta"), ratings);
	});
});

function addTitleObserver(node) {
	node.querySelectorAll(".jawBoneContent").forEach(function(node) {
		if (!node.hasAttribute("observed")) {
			jawBoneContentObserver.observe(node, observerOptions);
			node.setAttribute("observed", "true");
		};
	});
	node.querySelectorAll(".title-card-container > div > span").forEach(function(node) {
		if (!node.hasAttribute("observed")) {
			titleCardObserver.observe(node, observerOptions);
			node.setAttribute("observed", "true");
		};
	});
}

var rowObserver = new MutationObserver(function(mutations, observer) {
	mutations.forEach(function(mutation) {
		if (mutation.addedNodes) {
			mutation.addedNodes.forEach(function(node) {
				if (node.nodeType === 1) {
					addTitleObserver(node);
				}
			});
		}
	});
});

var mainObserver = new MutationObserver(function(mutations, observer) {
	var mainView = document.querySelector(".mainView");
	if (mainView) {
		observer.disconnect();
		rowObserver.observe(mainView, observerOptions);
		addTitleObserver(mainView);
		addFeaturedInfo(mainView);
	}
});

function addFeaturedInfo(node) {
	var jawBone = node.querySelector(".jawBoneContainer > .jawBone");
	if (jawBone) {
		getInfoAndRatings(jawBone.querySelector(".title"), null, function(ratings) {
			injectRatings(node.querySelector(".meta"), ratings);
		});
	}
}

var playerObserver = new MutationObserver(function(mutations, observer) {
	var playerTitle = document.querySelector(".player-status-main-title");
	if (playerTitle && playerTitle.textContent && playerTitle.textContent.length > 0) {
		observer.disconnect();
		addPlayerInfo(playerTitle);
	}
});

function addPlayerInfo(playerTitle) {
	if (playerTitle) {
		var infoNode = playerTitle.parentNode;
		var episodeSpan;
		Array.prototype.some.call(infoNode.getElementsByTagName('span'), function(span) {
			if (span.classList.length == 0) {
				episodeSpan = span;
				return true;
			}
		});
		getInfoAndRatings(playerTitle, episodeSpan, function(ratings) {
			injectRatings(infoNode, ratings);
		});
	}
}

var episodeObserver = new MutationObserver(function(mutations, observer) {
	var episodeListContainer = document.querySelector(".episode-list-container");
	if (episodeListContainer) {
		observer.disconnect();
		addEpisodeInfo(episodeListContainer);
	}
});

function addEpisodeInfo(episodeListContainer) {
	var title = document.querySelector(".player-status-main-title").textContent;
	var season = extractSeasonNumber(episodeListContainer.querySelector(".seasons-title").textContent);
	var episodes = episodeListContainer.querySelectorAll(".episode-list-index");
	episodes.forEach(function(episode) {
		getRatings(title, season, episode.textContent, function(ratings) {
			injectRatings(episode.parentNode, ratings);
		});
	});
}

function extractSeasonNumber(text) {
	var regex = /(S|s)eason (\d+)/
	var match = regex.exec(text);
	return match[2];
}

if (mainView = document.querySelector(".mainView")) {
	rowObserver.observe(mainView, observerOptions);
	addTitleObserver(mainView);
	addFeaturedInfo(mainView);
} else if (playerTitle = document.querySelector(".player-status-main-title")) {
	console.log("Found player title");
	if (playerTitle && playerTitle.textContent && playerTitle.textContent.length > 0) {
		addPlayerInfo(playerTitle);
	}
} else {
	mainObserver.observe(document, observerOptions);
	playerObserver.observe(document, observerOptions);
	episodeObserver.observe(document, observerOptions);
}<commit_msg>Handle episode selector season change<commit_after>var lastTitle = "";

function getInfo(titleNode, episodeNode, callback) {
	var info = {};
	if (titleNode) {
		info["title"] = titleNode.textContent;
	}
	if (episodeNode) {
		var text = episodeNode.textContent;
		var regex = /\D*(\d+)\D*(\d+)/
		var match = regex.exec(text);
		info["season"] = match[1];
		info["episode"] = match[2];
	}
	callback(info);
}

function getRatings(title, season, episode, callback) {
	if (title && title.length && (!lastTitle || lastTitle == title || !title.endsWith(lastTitle))) {
		lastTitle = title;
		fetchRatings(title, season, episode, function(ratings) {
			callback(ratings);
		});
	}
}

function getInfoAndRatings(titleNode, episodeNode, callback) {
	getInfo(titleNode, episodeNode, function(info) {
		getRatings(info["title"], info["season"], info["episode"], callback);
	})
}

MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

var observerOptions = {
	childList: true,
	subtree: true
}

var jawBoneContentObserver = new MutationObserver(function(mutations, observer) {
	var node = mutations[mutations.length - 1].target;
	getInfoAndRatings(node.querySelector(".jawBone > h3"), null, function(ratings) {
		injectRatings(node.querySelector(".meta"), ratings);
	});
});

var titleCardObserver = new MutationObserver(function(mutations, observer) {
	var node = mutations[mutations.length - 1].target;
	getInfoAndRatings(node.querySelector(".bob-title"), null, function(ratings) {
		injectRatings(node.querySelector(".meta"), ratings);
	});
});

function addTitleObserver(node) {
	node.querySelectorAll(".jawBoneContent").forEach(function(node) {
		if (!node.hasAttribute("observed")) {
			jawBoneContentObserver.observe(node, observerOptions);
			node.setAttribute("observed", "true");
		};
	});
	node.querySelectorAll(".title-card-container > div > span").forEach(function(node) {
		if (!node.hasAttribute("observed")) {
			titleCardObserver.observe(node, observerOptions);
			node.setAttribute("observed", "true");
		};
	});
}

var rowObserver = new MutationObserver(function(mutations, observer) {
	mutations.forEach(function(mutation) {
		if (mutation.addedNodes) {
			mutation.addedNodes.forEach(function(node) {
				if (node.nodeType === 1) {
					addTitleObserver(node);
				}
			});
		}
	});
});

var mainObserver = new MutationObserver(function(mutations, observer) {
	var mainView = document.querySelector(".mainView");
	if (mainView) {
		observer.disconnect();
		rowObserver.observe(mainView, observerOptions);
		addTitleObserver(mainView);
		addFeaturedInfo(mainView);
	}
});

function addFeaturedInfo(node) {
	var jawBone = node.querySelector(".jawBoneContainer > .jawBone");
	if (jawBone) {
		getInfoAndRatings(jawBone.querySelector(".title"), null, function(ratings) {
			injectRatings(node.querySelector(".meta"), ratings);
		});
	}
}

var playerObserver = new MutationObserver(function(mutations, observer) {
	var playerTitle = document.querySelector(".player-status-main-title");
	if (playerTitle && playerTitle.textContent && playerTitle.textContent.length > 0) {
		observer.disconnect();
		addPlayerInfo(playerTitle);
	}
});

function addPlayerInfo(playerTitle) {
	if (playerTitle) {
		var infoNode = playerTitle.parentNode;
		var episodeSpan;
		Array.prototype.some.call(infoNode.getElementsByTagName('span'), function(span) {
			if (span.classList.length == 0) {
				episodeSpan = span;
				return true;
			}
		});
		getInfoAndRatings(playerTitle, episodeSpan, function(ratings) {
			injectRatings(infoNode, ratings);
		});
	}
}

var lastSeason = "";

var episodeContainerObserver = new MutationObserver(function(mutations, observer) {
	var episodeListContainer = document.querySelector(".episode-list-container");
	if (episodeListContainer) {
		addEpisodeInfo(episodeListContainer);
	}
});

function addEpisodeInfo(episodeListContainer) {
	var title = document.querySelector(".player-status-main-title").textContent;
	var seasonNode = episodeListContainer.querySelector(".seasons-title");
	var season = extractSeasonNumber(seasonNode.textContent);
	if (season && season != lastSeason) {
		lastSeason = season;
		var episodes = episodeListContainer.querySelectorAll(".episode-list-index");
		episodes.forEach(function(episode) {
			getRatings(title, season, episode.textContent, function(ratings) {
				injectRatings(episode.parentNode, ratings);
			});
		});
	}
}

function extractSeasonNumber(text) {
	var regex = /(S|s)eason (\d+)/
	var match = regex.exec(text);
	if (match) {
		return match[2];
	}
	return null;
}

if (mainView = document.querySelector(".mainView")) {
	rowObserver.observe(mainView, observerOptions);
	addTitleObserver(mainView);
	addFeaturedInfo(mainView);
} else if (playerTitle = document.querySelector(".player-status-main-title")) {
	console.log("Found player title");
	if (playerTitle && playerTitle.textContent && playerTitle.textContent.length > 0) {
		addPlayerInfo(playerTitle);
	}
} else {
	mainObserver.observe(document, observerOptions);
	playerObserver.observe(document, observerOptions);
	episodeContainerObserver.observe(document, observerOptions);
}
{'fraction_non_alphanumeric': 0.14698292973402144, 'fraction_numerical': 0.001984914648670107, 'mean_word_length': 5.781292059219381, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3487075', 'n_tokens_mistral': 3370, 'n_tokens_neox': 3158, 'n_words': 540}