/* send bookmarks data.  */

function getBookmarksText(doc) {
	var candidates = ["P", "B", "I", "U", "H1", "H2", "H3", "A", "PRE", "CODE", "SPAN"];
	var ignore = ["LINK", "STYLE", "SCRIPT", "NOSCRIPT"];
	var text = "";
	var pageElements = doc.querySelectorAll("*");
	for (var i = 0; i < pageElements.length; i++) {

		var el = pageElements[i]
		if (candidates.indexOf(el.tagName) != -1 || (!el.childElementCount && ignore.indexOf(el.tagName) == -1)) {
			text += " " + el.textContent;
		}
	}

	//special meta tags

	var mt = document.querySelector("meta[name=description]");

	if (mt) {
		text += " " + mt.content;
	}

	text = text.replace(/[\n\t]/g, ""); //remove useless newlines/tabs that increase filesize

	text = text.replace(/\s{2,}/g, " "); //collapse multiple spaces into one
	text = text.replace(/<.*?>/g, "");

	return text;
}

ipc.on("sendData", function () {
	var text = getBookmarksText(document);

	//try to also extract text for same-origin iframes (such as the reader mode frame)

	var frames = document.querySelectorAll("iframe");

	for (var x = 0; x < frames.length; frames++) {
		try {
			text += ". " + getBookmarksText(frames[x].contentDocument);
		} catch (e) {}
	}

	/* also parse special metadata: price, rating, location */

	var price, rating, location;

	//pricing

	var priceEl = document.querySelector("[itemprop=price], .price, .offer-price, #priceblock_ourprice, .discounted-price");
	var currencyEl = document.querySelector("[itemprop=priceCurrency], [property=priceCurrency]");

	if (priceEl) {
		price = priceEl.textContent;
	}

	if (!/\d/g.test(price)) { //if the price doesn't contain a number, it probably isn't accurate
		price = undefined;
	}

	if (price && price.indexOf("$") == -1 && currencyEl) { //try to add a currency if we don't have one. We should probably check for other currencies, too.
		price = (currencyEl.content || currencyEl.textContent).replace("USD", "$") + price;
	}

	//ratings

	var ratingEl = document.querySelector('.star-img, .rating, [itemprop="ratingValue"], [property="ratingValue"]');

	if (!ratingEl) { //if we didn't find an element, try again with things that might be a rating element, but are less likely
		ratingEl = document.querySelector('[class^="rating"], [class^="review"]');
	}


	if (ratingEl) {
		rating = ratingEl.title || ratingEl.alt || ratingEl.content || ratingEl.textContent;
		rating = rating.replace("rating", "").replace("stars", "").replace("star", "").trim();
		console.log("r: " + rating);
		if (rating && /\d+$/g.test(rating)) { //if the rating ends in a number, we assume it means "stars", and append the prefix
			rating = rating + " stars";
		}
	}

	//location

	var locationEl = document.querySelector('[itemprop="location"], [itemprop="address"]');

	if (!locationEl) {
		var locationEl = document.querySelector('.adr, .addr, .address');
	}

	if (locationEl) {
		location = locationEl.textContent.trim();
	}

	//remove US postcodes, since these usually aren't important and they take up space
	//TODO make this work for other countries
	if (location && /,?\d{5}$/g.test(location)) {
		location = location.replace(/,?\d{5}$/g, "");
	}


	console.log("rating: " + rating);
	console.log("price: " + price);
	console.log("location: " + location);

	ipc.sendToHost("bookmarksData", {
		url: window.location.toString(),
		title: document.title || "",
		text: text,
		extraData: {
			metadata: {
				price: price,
				rating: rating,
				location: location,
			}
		}
	});
});

@@ -1239,16 +1239,30 @@
 location
+, cooking time
  */%0A%0A%09va
@@ -1286,16 +1286,26 @@
 location
+, cookTime
 ;%0A%0A%09//pr
@@ -3153,16 +3153,325 @@
 %22);%0A%09%7D%0A%0A
+%09//cooking time%0A%0A%09var cookingTimeEl = document.querySelector('%5Bitemprop=%22totalTime%22%5D, %5Bitemprop=%22cookTime%22%5D');%0A%0A%09if (cookingTimeEl) %7B%0A%09%09cookTime = cookingTimeEl.textContent;%0A%09%09cookTime = cookTime.replace(/%5Csm$/g, %22 minutes%22).replace(/%5Csh$/g, %22 hours%22);%0A%09%09cookTime = cookTime.replace(%221 hours%22, %221 hour%22);%0A%09%7D%0A%0A
 %0A%09consol
@@ -3778,16 +3778,40 @@
 cation,%0A
+%09%09%09%09cookTime: cookTime,%0A
 %09%09%09%7D%0A%09%09%7D

add cookingTime