Source for "One Simple Manga Ajax Viewer"

By InstantKarma
Has no other scripts.


Add Syntax Highlighting (this will take a few seconds, probably freezing your browser while it works)

// ==UserScript==
// @name           One Simple Manga Ajax Viewer
// @namespace      InstantKarma
// @description    This script hides everything in the page except for the picture and the bottom navigation buttons on a black background and preloads both the next and previous pictures. The picture change is done without reloading of the page, using a transition effect.
// @include        http://www.onemanga.com/*/*/
// @exclude    http://www.onemanga.com/
// @exclude    http://www.onemanga.com/directory/*
// ==/UserScript==

//css styles
css = ":focus { -moz-outline-style: none; } body { color: white !important;} #content2 { background-color:black !important;padding:0px } #content2 h1 {padding-bottom:0px !important;}h1 {margin-bottom:0px !important;} img.manga-page {border:0px none !important;}";
//add them to the page
GM_addStyle(css);

/*
Function which embeds a javascript function into the html so that 
you can use it after the page has loaded
(modified from original code borrowed from http://wiki.greasespot.net/Code_snippets)
*/
var scriptElement;
function embedFunction(s) {
	if (scriptElement == undefined) {
	scriptElement = document.createElement('script');
	}
	scriptElement.innerHTML = scriptElement.innerHTML + '\n' + s.toString().replace(/([\s\S]*?return;){2}([\s\S]*)}/,'$2');
}

/*
Function which sets all the variables holding the next and 
previous pages' information (this page's too) as well and preloads
the next and previous images. Called from the page's body's onLoad
attribute.
*/

function init() {
	var nodes = document.getElementById('content2').childNodes;
	for (i = 0; i < nodes.length; i++){
		if (nodes[i].getAttribute != undefined && nodes[i].getAttribute('class') == 'warning'){
			window.location = window.location.href + '?no_warn=1';
		}
	}
	hideStuff();
	hidePreviousButton = false;
	hideNextButton = false;
	wentUpOneChapter = false;
	wentDownOneChapter = false;
	fromBookMark = false;
	insertTable();
	createMangaSelect();
	document.images[0].id = "manga-page";//set an id for the image
	//set this page's url and html
	goToPage(window.location.href);
	//assign the functions to browse the pages to the image and buttons
	document.onkeydown = onKeyDown;
	bottomButtons.childNodes[1].onclick = goToPreviousPage;
	bottomButtons.childNodes[3].onclick = goToNextPage;
	topMenuDiv.childNodes[7].onclick = goToPreviousPage;
	topMenuDiv.childNodes[9].onclick = goToNextPage;
	var option = document.createElement('OPTION');
	option.setAttribute('value', '-1');
	option.innerHTML = 'Select a page...';
	topMenuDiv.childNodes[5].insertBefore(option , topMenuDiv.childNodes[5].firstChild);
	if (hidePreviousButton){
		bottomButtons.childNodes[1].setAttribute('disabled', '');
		topMenuDiv.childNodes[7].setAttribute('disabled', '');
	}
	if (hideNextButton){
		bottomButtons.childNodes[3].setAttribute('disabled', '');
		topMenuDiv.childNodes[9].setAttribute('disabled', '');
	}
}

function goToPage(targetURL){
	thisPage = targetURL;
	thisPageHTML = getPageHTML(thisPage);
	document.title = getPageTitle(thisPageHTML);
	document.images[0].style.opacity = 0;
	thisPageImage = new Image();
	thisPageImage.onload = function(){
		document.images[0].src=this.src;
		document.images[0].width=this.width;
		document.images[0].height=this.height;
		//fade it in
		opacity("manga-page", 0, 100, 200);
		//go to top
		window.scrollTo(0,0);
	};
	thisPageImage.src = getPageImage(thisPageHTML).src;
	//set next page's url, title, html and image
	nextPageURL = getNextPageURL(thisPageHTML);
	nextPageHTML = getPageHTML(nextPageURL);
	if (nextPageHTML == null){
		hideNextButton = true;
		bottomButtons.childNodes[3].setAttribute('disabled', '');
		topMenuDiv.childNodes[9].setAttribute('disabled', '');
		nextPageTitle = "";
		nextImage = new Image();
	} else {
		hideNextButton = false;
		bottomButtons.childNodes[3].removeAttribute('disabled');
		topMenuDiv.childNodes[9].removeAttribute('disabled');
		nextPageTitle = getPageTitle(nextPageHTML);
		nextImage = new Image();
		nextImage.src = getPageImage(nextPageHTML).src;
	}
	//set previous page's url, title, html and image
	previousPageURL = getPreviousPageURL(thisPageHTML);
	previousPageHTML = getPageHTML(previousPageURL);
	if (previousPageHTML == null){
		hidePreviousButton = true;
		bottomButtons.childNodes[1].setAttribute('disabled', '');
		topMenuDiv.childNodes[7].setAttribute('disabled', '');
		previousPageTitle = "";
		previousImage = new Image();
	} else {
		hidePreviousButton = false;
		bottomButtons.childNodes[1].removeAttribute('disabled');
		topMenuDiv.childNodes[7].removeAttribute('disabled');
		previousPageTitle = getPageTitle(previousPageHTML);
		previousImage = new Image();
		previousImage.src = getPageImage(previousPageHTML).src;
	}
	topMenuDiv.childNodes[5].disabled = false;
}

function getBookMarks(){
	return eval(GM_getValue('bookmarks'));
}

function saveBookMarks(bookMarksArray){
	window.setTimeout(GM_setValue, 0, 'bookmarks', bookMarksArray.toSource());
}

function addBookMark(){
	var bookMarkName = unsafeWindow.selectedManga + ' chapter ' + unsafeWindow.selectedChapter + ' page ' + unsafeWindow.selectedPage;
	unsafeWindow.bookMarksArray.push([unsafeWindow.thisPage, bookMarkName]);
	var bookMarkLink = document.createElement('A');
	bookMarkLink.href = "javascript:goToBookMark('"+unsafeWindow.thisPage+"');";
	bookMarkLink.innerHTML = bookMarkName;
	var bookMarkLinkDelete = document.createElement('A');
	bookMarkLinkDelete.href = "javascript:deleteBookMark('"+(unsafeWindow.bookMarksArray.length - 1) +"');";
	bookMarkLinkDelete.innerHTML = 'delete';
	var bookMarksDiv = document.getElementById('bookmarklinks');
	bookMarksDiv.appendChild(document.createElement('BR'));
	bookMarksDiv.appendChild(bookMarkLink);
	bookMarksDiv.appendChild(document.createTextNode('   '));
	bookMarksDiv.appendChild(bookMarkLinkDelete);
	saveBookMarks(unsafeWindow.bookMarksArray);
}

function createMangaSelect() {
	mangaSelect = document.createElement('SELECT');
	mangaSelect.onchange = onMangaTitleChange;
	mangaSelect.style.width = "150px";
	for (var i=0; i<manga_array.length; i++) {
		mangaSelect.options[i] = new Option(manga_array[i][0],manga_array[i][1]);
	}
	selectedManga = window.location.href.split("/")[3];
	selectedMangaNumber = Number(document.images[0].src.split("/")[4]);
	for (i=0; i<mangaSelect.options.length; i++){
		if (mangaSelect.options[i].value == selectedManga){
			mangaSelect.options[i].selected = true;
			break;
		}
	}
	topMenuDiv.insertBefore(mangaSelect, topMenuDiv.firstChild);
	bookMarksButton = document.createElement('INPUT');
	bookMarksButton.setAttribute('type', 'button');
	bookMarksButton.setAttribute('value', 'Bookmarks');
	bookMarksButton.onclick = function() {
								var bookMarksDiv = document.getElementById('bookmarks');
								bookMarksDiv.style.display = "";
								for (i=0;i<bookMarksDiv.childNodes.length;i++) {
									bookMarksDiv.childNodes[i].style.display = "";
								}
								};
	topMenuDiv.insertBefore(bookMarksButton, topMenuDiv.firstChild);
	topMenuDiv.childNodes[3].setAttribute('onchange', 'javascript:onMangaChapterChange()');
	topMenuDiv.childNodes[3].onchange = onMangaChapterChange;
	topMenuDiv.childNodes[5].setAttribute('onchange', 'javascript:onMangaPageChange()');
	topMenuDiv.childNodes[5].onchange = onMangaPageChange;
	selectedChapter = document.getElementById('id_chapter_1').value;
	selectedPage = topMenuDiv.childNodes[5].value;
}

function onMangaTitleChange() {
	selectedManga = this.value;
	getMangaChapters(selectedManga, 1);
}

function getMangaChapters(mangaTitle, firstPage){
	var firstMangaPageRequest = new XMLHttpRequest();
	firstMangaPageRequest.onreadystatechange = function(response){
		if (firstMangaPageRequest.readyState == 4){
			if (firstMangaPageRequest.status == 200) {
				var html = firstMangaPageRequest.responseText;
				var imageURL = getImageURL(html);
				selectedMangaNumber = Number(imageURL.split("/")[4]);
				var mangaJSRequest = new XMLHttpRequest();
				mangaJSRequest.onreadystatechange = function(response){
					if (mangaJSRequest.readyState == 4){
						var js = mangaJSRequest.responseText;
						document.getElementById('id_chapter_1').innerHTML = "";
						if (document.getElementById('id_chapter_2')){
							document.getElementById('id_chapter_2').innerHTML = "";
						}
						topMenuDiv.childNodes[5].innerHTML = '<option name="Select" value=""/>';
						eval(js);
						if (fromBookMark){
							for (i=0; i<topMenuDiv.childNodes[3].options.length;i++){
								if (topMenuDiv.childNodes[3].options[i].value == selectedChapter){
									topMenuDiv.childNodes[3].options.selectedIndex = i;
									topMenuDiv.childNodes[3].onchange();
									break;
								}
							}
						}
						selectedChapter = document.getElementById('id_chapter_1').value;
						var chapterPagesRequest = new XMLHttpRequest();
						var url = '/'+mangaTitle+'/'+ selectedChapter + '/';
						chapterPagesRequest.onreadystatechange = function(response) {
							if (chapterPagesRequest.readyState == 4){
								var startTag = '<div class="chapter-navigation">';
								var endTag = '<div class="one-page">';
								var tempHTML = getTagContents(chapterPagesRequest.responseText, startTag, endTag);
								startTag = '<select name="page" class="page-select" onchange="javascript:window.location=\'/' + mangaTitle + '/' + selectedChapter + '/\'+this.value+\'/\';">';
								endTag = '</select>';
								chapterOptionsHTML = tempHTML.substring(tempHTML.indexOf(startTag)+startTag.length, tempHTML.lastIndexOf(endTag));
								topMenuDiv.childNodes[5].innerHTML = chapterOptionsHTML;
								var option = document.createElement('OPTION');
								option.setAttribute('value', '-1');
								option.innerHTML = 'Select a page...';
								topMenuDiv.childNodes[5].insertBefore(option , topMenuDiv.childNodes[5].firstChild);
								topMenuDiv.childNodes[5].value = -1;
							}
						};
						chapterPagesRequest.open("GET", url, true);
						chapterPagesRequest.send(null);
					}
				};
				mangaJSRequest.open("GET", '/manga_js/'+selectedMangaNumber+'/', true);
				mangaJSRequest.send(null);
			} else {
				if (firstPage != 0) {
					getMangaChapters(mangaTitle, 0);
				} else {
					return;
				}
			}
		}
	};
	firstMangaPageRequest.open("GET", 'http://www.onemanga.com/'+mangaTitle+'/'+firstPage+'/?no_warn=1', true);
	firstMangaPageRequest.send(null);
}

function onMangaChapterChange() {
	topMenuDiv.childNodes[5].disabled = true;
	selectedChapter = this.value;
	var mangaJSRequest = new XMLHttpRequest();
	mangaJSRequest.onreadystatechange = function(response){
		if (mangaJSRequest.readyState == 4){
			var js = mangaJSRequest.responseText;
			document.getElementById('id_chapter_1').innerHTML = "";
			if (document.getElementById('id_chapter_2')){
				document.getElementById('id_chapter_2').innerHTML = "";
			}
			eval(js);
			document.getElementById('id_chapter_1').value = selectedChapter;
			if (document.getElementById('id_chapter_2')){
				document.getElementById('id_chapter_2').value = selectedChapter;
			}
			var chapterPagesRequest = new XMLHttpRequest();
			var url = '/'+selectedManga+'/'+ selectedChapter + '/?no_warn=1';
			chapterPagesRequest.onreadystatechange = function(response) {
				if (chapterPagesRequest.readyState == 4){
					var startTag = '<div class="chapter-navigation">';
					var endTag = '<div class="one-page">';
					var tempHTML = getTagContents(chapterPagesRequest.responseText, startTag, endTag);
					startTag = '<select name="page" class="page-select" onchange="javascript:window.location=\'/' + selectedManga + '/' + selectedChapter + '/\'+this.value+\'/\';">';
					endTag = '</select>';
					chapterOptionsHTML = tempHTML.substring(tempHTML.indexOf(startTag)+startTag.length, tempHTML.lastIndexOf(endTag));
					topMenuDiv.childNodes[5].innerHTML = chapterOptionsHTML;
					var option = document.createElement('OPTION');
					option.setAttribute('value', '-1');
					option.innerHTML = 'Select a page...';
					topMenuDiv.childNodes[5].insertBefore(option , topMenuDiv.childNodes[5].firstChild);
					if (wentUpOneChapter) {
						topMenuDiv.childNodes[5].options.selectedIndex = 1;
						selectedPage = topMenuDiv.childNodes[5].value;
						wentUpOneChapter = false;
					} else if (wentDownOneChapter) {
						topMenuDiv.childNodes[5].options.selectedIndex = topMenuDiv.childNodes[5].options.length - 1;
						selectedPage = topMenuDiv.childNodes[5].value;
						wentDownOneChapter = false;
					} else if (fromBookMark) {
						for (i=0; i<topMenuDiv.childNodes[5].options.length;i++){
							if (topMenuDiv.childNodes[5].options[i].value == selectedPage){
								topMenuDiv.childNodes[5].options.selectedIndex = i;
								break;
							}
						}
						fromBookMark = false;
					} else {
						topMenuDiv.childNodes[5].options.selectedIndex = 0;
					}
					topMenuDiv.childNodes[5].disabled = false;
				}
			};
			chapterPagesRequest.open("GET", url, true);
			chapterPagesRequest.send(null);
		}
	};
	mangaJSRequest.open("GET", '/manga_js/'+selectedMangaNumber+'/', true);
	mangaJSRequest.send(null);
}

function onMangaPageChange() {
	if (this.value != -1){
		selectedPage = this.value;
		bottomButtons.childNodes[3].removeAttribute('disabled');
		topMenuDiv.childNodes[9].removeAttribute('disabled');
		hideNextButton = false;
		bottomButtons.childNodes[1].removeAttribute('disabled');
		topMenuDiv.childNodes[7].removeAttribute('disabled');
		hidePreviousButton = false;
		goToPage('/'+selectedManga+'/'+selectedChapter+'/'+selectedPage);
	} else {
		return;
	}
}

function insertTable(){
	link = document.getElementById('nextPageLink');
	table = document.createElement('TABLE');
	table.setAttribute('width', '100%');
	tr = document.createElement('TR');
	emptyTd1 = document.createElement('TD');
	emptyTd1.setAttribute('width','50%');
	emptyTd1.setAttribute('align', 'left');
	imageTd = document.createElement('TD');
	imageTd.setAttribute('align', 'center');
	imageDiv = document.createElement('DIV');
	if (link == null){
		link = document.createElement('A');
		link.setAttribute('href', 'javascript:goToNextPage()');
		link.setAttribute('id', 'nextPageLink');
		link.innerHTML = '<img id="manga-page" class="manga-page" src='+ document.images[0].src +' alt="">';
	}	
	imageDiv.appendChild(link);
	imageTd.appendChild(imageDiv);
	imageTd.appendChild(bottomButtons);
	emptyTd2 = document.createElement('TD');
	emptyTd2.setAttribute('width','50%');
	emptyTd2.setAttribute('align', 'right');
	tr.appendChild(emptyTd1);
	tr.appendChild(imageTd);
	tr.appendChild(emptyTd2);
	table.appendChild(tr);
	onePageDiv.innerHTML="";
	onePageDiv.appendChild(table);
}
/*
Function which hides all the unnecessary divs from the page, and loads the 
*/
function hideStuff(){
divs = document.getElementsByTagName("div");
h1s = document.getElementsByTagName("h1");
h1s[0].innerHTML="";//hide the manga title
i=0;
usedDivs = new Array();
var j = 0;
while(i<divs.length){//for every div		
	class=divs[i].getAttribute("class");//get the class
	if(class!="one-page" && class != "chapter-navigation"){//hide everything except for the important divs
		divs[i].style.display="none";
	} else {//for the important divs
		usedDivs[j] = divs[i];//save them so we can get the buttons div later
		j++;
	}
	i++;	
}
onePageDiv = usedDivs[1];
onePageDiv.setAttribute('id', "one-page");
topMenuDiv = usedDivs[0];
bottomButtons = usedDivs[2];
for (i = 0; i<onePageDiv.childNodes.length; i++){
	if (onePageDiv.childNodes[i].tagName == 'A'){
		onePageDiv.childNodes[i].href = "javascript:goToNextPage()";
		onePageDiv.childNodes[i].setAttribute('id', 'nextPageLink');
	}
}
//topMenuDiv.style.display = 'none';
//show necessary divs
document.getElementById("wrap2").style.display="";
document.getElementById("content").style.display="";
document.getElementById("content2").style.display="";
}

function onKeyDown(e){
    if (e.which) {
        keycode = e.which;
    } else {
        keycode = e.keyCode;
    }
    if (keycode == 39 || keycode == 68 ) {
		goToNextPage();
        return false;
    } else if (keycode == 37 || keycode == 65) {
		goToPreviousPage();
        return false;
    }
}

/*
Function to get the picture from the next page and load it in the page's img.
*/

function goToNextPage() {
	if (hidePreviousButton) {
		bottomButtons.childNodes[1].removeAttribute('disabled');
		topMenuDiv.childNodes[7].removeAttribute('disabled');
		hidePreviousButton = false;
	}
	if (hideNextButton){
		return;
	}
	if (topMenuDiv.childNodes[5].options.selectedIndex < topMenuDiv.childNodes[5].options.length-1) {
		topMenuDiv.childNodes[5].options.selectedIndex++;
		selectedPage = topMenuDiv.childNodes[5].value;
	} else {
		wentUpOneChapter = true;
		topMenuDiv.childNodes[3].options.selectedIndex--;
		topMenuDiv.childNodes[3].onchange();
	}
	//this page is now the previous page
	previousPageTitle = document.title;
	previousImage.src = document.images[0].src;
	previousPageURL = thisPage;
	previousPageHTML = thisPageHTML;
	//hide this image
	document.getElementById("manga-page").style.opacity = 0;
	//change the title (todo: extract the real titles from the html)
	document.title = nextPageTitle;
	//create a new image
	var image = new Image();
	//when image finishes loading
	image.onload = function (evt) {
		//put this image in the existing one
		document.images[0].src=this.src;
		document.images[0].width=this.width;
		document.images[0].height=this.height;
		//fade it in
		opacity("manga-page", 0, 100, 200);
		//go to top
		window.scrollTo(0,0);
	    }
	//the next page is now this page
	image.src=nextImage.src;
	thisPage = nextPageURL;
	thisPageHTML = nextPageHTML;
	//get next page and start preloading the next image
	nextPageURL = getNextPageURL(nextPageHTML);
	nextPageHTML = getPageHTML(nextPageURL);
	if (nextPageHTML == null){
		hideNextButton = true;
		bottomButtons.childNodes[3].setAttribute('disabled', '');
		topMenuDiv.childNodes[9].setAttribute('disabled', '');
	} else {
		nextPageTitle = getPageTitle(nextPageHTML);
		var image = getPageImage(nextPageHTML);
		nextImage.src = image.src;
	}
}

/*
Function to get the picture from the previous page and load it in the page's img.
*/

function goToPreviousPage() {
	//this page is now the next page
	if (hideNextButton){
		bottomButtons.childNodes[3].removeAttribute('disabled');
		topMenuDiv.childNodes[9].removeAttribute('disabled');
		onePageDiv.childNodes[0].childNodes[0].childNodes[1].childNodes[0].childNodes[0].href = "javascript:goToNextPage()";
		hideNextButton = false;
	}
	if (hidePreviousButton){
		return;
	}
	if (topMenuDiv.childNodes[5].options.selectedIndex > 1) {
		topMenuDiv.childNodes[5].options.selectedIndex--;
		selectedPage = topMenuDiv.childNodes[5].value;
	} else {
		wentDownOneChapter = true;
		topMenuDiv.childNodes[3].options.selectedIndex++;
		topMenuDiv.childNodes[3].onchange();
	}
	nextPageTitle = document.title;
	nextImage.src = document.images[0].src;
	nextPageURL = thisPage;
	nextPageHTML = thisPageHTML;
	//hide this image
	document.getElementById("manga-page").style.opacity = 0;
	document.title = previousPageTitle;
	//create a new image
	var image = new Image();
	//when image finishes loading
	image.onload = function (evt) {
		document.images[0].src=this.src;
	        document.images[0].width=this.width;
	        document.images[0].height=this.height;
		opacity("manga-page", 0, 100, 200);
		window.scrollTo(0,0);
	    }
	//the previous page is now this page
	image.src=previousImage.src;
	thisPage = previousPageURL;
	thisPageHTML = previousPageHTML;
	//get the previous page and start preloading the previous image.
	previousPageURL = getPreviousPageURL(previousPageHTML);
	previousPageHTML = getPageHTML(previousPageURL);
	if (previousPageHTML == null){
		hidePreviousButton = true;
		bottomButtons.childNodes[1].setAttribute('disabled', '');
		topMenuDiv.childNodes[7].setAttribute('disabled', '');
	} else {
		previousPageTitle = getPageTitle(previousPageHTML);
		var image = getPageImage(previousPageHTML);
		previousImage.src = image.src;
	}
}
/*
Function that gets the image from a page's html and returns an image object
*/
function getPageImage(pageHTML){
	var imageURL = getImageURL(pageHTML);
	image = new Image();
	image.src = imageURL;
	return image;
}

/*
Function which makes the ajax request to get the HTML from a given URL and returns it.
*/

function getPageHTML(url) {
	var request = new XMLHttpRequest();
	//synchronous request. It's getting HTML, shouldn't take too long.
	request.open("GET",url + '?no_warn=1', false);
	request.send(null);
	if (request.status == 404){
		return null;
	} else {
		return request.responseText;
	}
}

/*
Function which extracts the image URL from a given HTML
*/

function getImageURL(html){
	var startIndex = html.indexOf('manga-page')+17;
	var endIndex = html.indexOf('jpg')+3;
	return html.substring(startIndex, endIndex);
}

/*
Function which extracts the page title of a given HTML
*/

function getPageTitle(html){
	return getTagContents(html, '<title>', '</title>');
}

function getTagContents(html, startTag, endTag){
	return html.substring(html.indexOf(startTag)+startTag.length, html.indexOf(endTag));
}

/*
Function which extracts the previous page's URL from a given HTML
*/

function getPreviousPageURL(html) {
	var startIndex = html.indexOf('keycode == 37) {')+44;
	var endIndex = html.indexOf("';", startIndex);
	return html.substring(startIndex, endIndex);
}

/*
Function which extracts the next page's URL from a given HTML
*/

function getNextPageURL(html) {
	var startIndex = html.indexOf('window.location = ')+19;
	var endIndex = html.indexOf("else if (keycode")-33;
	return html.substring(startIndex, endIndex);
}

/*
Functions to do the fade in borrowed from http://brainerror.net/scripts/javascript/blendtrans/
*/

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    opacity = (opacity == 100)?99:opacity;
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

//embed functions into the html
embedFunction(init);
embedFunction(getPageHTML);
embedFunction(getImageURL);
embedFunction(getPageImage);
embedFunction(getNextPageURL);
embedFunction(goToNextPage);
embedFunction(getPreviousPageURL);
embedFunction(goToPreviousPage);
embedFunction(opacity);
embedFunction(changeOpac);
embedFunction(hideStuff);
embedFunction(getPageTitle);
embedFunction(onKeyDown);
embedFunction(insertTable);
embedFunction(getTagContents);
embedFunction(createMangaSelect);
embedFunction(goToPage);
embedFunction(onMangaTitleChange);
embedFunction(onMangaChapterChange);
embedFunction(onMangaPageChange);
embedFunction(getMangaChapters);
embedFunction(saveBookMarks);
embedFunction(getBookMarks);
embedFunction(deleteBookMark);
embedFunction(goToBookMark);

function deleteBookMark(index){
	bookMarksArray.splice(index, 1);
	var bookMarkLinksDiv = document.getElementById('bookmarklinks');
	bookMarkLinksDiv.innerHTML = "";
	for (i=0; i< bookMarksArray.length; i++) {
		var bookMarkLink = document.createElement('A');
		bookMarkLink.href = "javascript:goToBookMark('"+ bookMarksArray[i][0] +"');";
		bookMarkLink.innerHTML = bookMarksArray[i][1];
		var bookMarkLinkDelete = document.createElement('A');
		bookMarkLinkDelete.href = 'javascript:deleteBookMark('+i+');';
		bookMarkLinkDelete.innerHTML = 'delete';
		bookMarkLinksDiv.appendChild(document.createElement('BR'));
		bookMarkLinksDiv.appendChild(bookMarkLink);
		bookMarkLinksDiv.appendChild(document.createTextNode('   '));
		bookMarkLinksDiv.appendChild(bookMarkLinkDelete);
	}
	document.getElementById('saveBookMarks').click();
}

function deleteBookMarkSave(){
	saveBookMarks(unsafeWindow.bookMarksArray);
}

function goToBookMark(url) {
	fromBookMark = true;
	urlArray = url.split('/');
	if (urlArray[0] == "http:"){
		urlArray.splice(0,3);
	} else {
		urlArray.splice(0,1);
	}
	selectedManga = urlArray[0];
	selectedChapter = urlArray[1];
	selectedPage = urlArray[2]==""?1:urlArray[2];
	for (i=0; i<topMenuDiv.childNodes[1].options.length;i++) {
		if (topMenuDiv.childNodes[1].options[i].value == selectedManga){
			topMenuDiv.childNodes[1].options.selectedIndex = i;
			topMenuDiv.childNodes[1].onchange();
			break;
		}
	}
	
	topMenuDiv.childNodes[5].selectedIndex = selectedPage;
	goToPage(url);
}

//set init() as onload for the body, and paint it black
bookmarksDiv = document.createElement('DIV');
bookmarksDiv.setAttribute('id', 'bookmarks');
bookmarksDiv.style.display = "none";
bookMarkLinksDiv = document.createElement('DIV');
bookMarkLinksDiv.setAttribute('id', 'bookmarklinks');
unsafeWindow.bookMarksArray = getBookMarks();
if (unsafeWindow.bookMarksArray){
	for (i=0; i< unsafeWindow.bookMarksArray.length; i++) {
		var bookMarkLink = document.createElement('A');
		bookMarkLink.href = "javascript:goToBookMark('"+ unsafeWindow.bookMarksArray[i][0] +"');";
		bookMarkLink.innerHTML = unsafeWindow.bookMarksArray[i][1];
		var bookMarkLinkDelete = document.createElement('A');
		bookMarkLinkDelete.href = 'javascript:deleteBookMark('+i+');';
		bookMarkLinkDelete.innerHTML = 'delete';
		bookMarkLinksDiv.appendChild(document.createElement('BR'));
		bookMarkLinksDiv.appendChild(bookMarkLink);
		bookMarkLinksDiv.appendChild(document.createTextNode('   '));
		bookMarkLinksDiv.appendChild(bookMarkLinkDelete);
	}
} else {
	unsafeWindow.bookMarksArray = new Array();
}
addBookMarkButton = document.createElement('INPUT');
addBookMarkButton.setAttribute('type', 'button');
addBookMarkButton.setAttribute('value', 'Bookmark this page');
addBookMarkButton.addEventListener('click', addBookMark, true);
saveBookMarksButton = document.createElement('INPUT');
saveBookMarksButton.setAttribute('type', 'button');
saveBookMarksButton.setAttribute('value', 'Save bookmarks');
saveBookMarksButton.setAttribute('id', 'saveBookMarks');
saveBookMarksButton.style.display = "none";
saveBookMarksButton.addEventListener('click', deleteBookMarkSave, true);
bookMarkButtonsDiv = document.createElement('DIV');
bookMarkButtonsDiv.appendChild(addBookMarkButton);
bookMarkButtonsDiv.appendChild(saveBookMarksButton);
bookmarksDiv.appendChild(bookMarkLinksDiv);
bookmarksDiv.appendChild(bookMarkButtonsDiv);
document.getElementById('content2').appendChild(bookmarksDiv);
document.body.appendChild(scriptElement);
document.body.setAttribute("onLoad", "init()");
document.body.bgcolor='FFFFFF';