User:Pavel Dusek/Sandbox/Copyvio Search Chromium Extension

From WikiLectures

An extension for Chromium/Google Chrome web browser that should make copyvio search much easier.

Install the file by clicking to this link: http://www.wikilectures.eu/extensions/ChromeExt/WLCopyvioSearch.crx. Then click on the icon in the upper right corner of the browser, type the title of the page you want to search for, click the run button and then just stare and be amazed. Usage of the standard browser shortcuts to switch tabs (Ctrl+Tab/Ctrl+Shift+Tab) and to close them (Ctrl+w) is recommended.

Source code

manifest.json

{
	"name": "WikiLectures Copyvio Search Extension",
	"version": "1.0",
	"description": "Extension that simplifies the search of text sources used on the project WikiLectures.eu",
	"browser_action": {
		"default_icon": "icon.png",
		"popup": "popup.html"
	},
	"permissions" : [
		"http://www.wikilectures.eu/*",
		"http://www.google.com/*",
		"http://www.google.cz/*",
		"tabs"
	]
}

popup.html

<html>
	<head>
		<script>
			//creating new global variable
			req = new XMLHttpRequest();
			function getPageText() {
				var exportUrl = 'http://www.wikilectures.eu/index.php?action=render&title=';
				var pageTitle = document.getElementById('pageTitleInput').value;
				req.open("GET", exportUrl + encodeURIComponent(pageTitle), true);
				req.onload = run;
				req.send(null);
			}
			function run() {
				//get the text from the wiki with action=render argument:
				var text = req.responseText;

				document.getElementById('text').innerHTML = text;
				//get just plain text:
				text = text.replace(/<table[\s\S]*?<\/table>/gmi, ''); //get rid of tables
				text = text.replace(/<span[\s\S]*?<\/span>/gmi, ''); //get rid of headlines and metadata
				text = text.replace(/<div[\s\S]*?<\/div>/gmi, ''); //get rid of metadata and images
				text = text.replace(/<(?:.|\n)*?>/gm, ''); //get rid of html tags

				//split into lines
				var lines = text.split("\n");
				var line = "";
				text = "";
				for (i in lines) {
					line = lines[i];
					//not empty string and longer than 30 characters:
					if (!line.match(/^\s*$/) && line.length > 30) {
						//search with the Google
						createTab("\"" + line + "\" -site:www.wikilectures.eu -wikilectures");
						text += lines[i] + "<br />";
					}
				}
			}
			
			//function that searches query with the Google in a new tab
			function createTab(query) {
				var url = "http://www.google.com/#pbx=1&q=";
				var createProperties = {"url": url + encodeURIComponent(query)};
				chrome.tabs.create(createProperties);
			}
		</script>
	</head>
	<body>
		<label for='pageTitleInput'>Title:</label><br />
		<input type='text' name='pageTitle' id='pageTitleInput' /><br />
		<button onClick='getPageText();'>Run</button><br />
		<span id='text'></span>
	</body>
</html>