Created
July 13, 2012 17:19
-
-
Save Dandu-Venkata-Ravi-Varma/3106092 to your computer and use it in GitHub Desktop.
Google Search Result Highlighting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function displayMessage(message, callback) { | |
// Display an alert-like message | |
var $ = $jquery; | |
if (isTesting) | |
console.log(message); | |
var bg = $('<div/>').appendTo("body"); | |
bg.css({ | |
"position" : "fixed", | |
"top" : 0, | |
"left" : 0, | |
"height" : "100%", | |
"width" : "100%", | |
"z-index" : 100000000001, | |
"background-color" : "rgba(130,130,130,0.5)" | |
}); | |
var box = $('<div/>').appendTo("body"); | |
box.css({ | |
"position" : "absolute", | |
"top" : "30%", | |
"left" : "30%", | |
"padding" : "5px", | |
"height" : "auto", | |
"width" : "25%", | |
"z-index" : 1000000000000, | |
"background-color" : "white", | |
"border" : "5px solid black" | |
}); | |
box.append(message); | |
setTimeout(function() { | |
box.fadeOut("slow", function() { | |
box.remove(); | |
bg.remove(); | |
callback(); | |
}); | |
}, 15000) | |
} | |
function logError(e) { | |
if (isTesting) | |
console.log("Error:", e); | |
} | |
function getHostName(href) { | |
// http://stackoverflow.com/a/736970 | |
var l = document.createElement("a"); | |
l.href = href; | |
return l.hostname.replace(/www\./gim, ""); | |
} | |
function useComment(data) { | |
if (isTesting) | |
console.log("UseComment", data); | |
data = { | |
"url" : "#token", | |
"comment" : "Congratulations 10% of your basket total will go towards UNICEF" | |
}; | |
displayMessage(data.comment, function() { | |
window.location = data.url; | |
}); | |
}; | |
function highlightGoogle(data) { | |
var links = $jquery(".vsc .r .l"); | |
var subLinks = $jquery(".sld .r .l"); | |
if (isTesting) | |
console.log("higlight", data, links, subLinks); | |
for ( var i = 0; i < links.length; i++) { | |
if ($jquery.inArray(links[i], subLinks) !== -1) | |
continue; | |
var link = $jquery(links[i]); | |
if (isTesting) | |
console.log(getHostName(link.attr("href")), $jquery.inArray( | |
getHostName(link.attr("href")), data), data); | |
if ($jquery.inArray(getHostName(link.attr("href")), data) !== -1) { | |
link.parent().parent().parent().css({ | |
"background-color" : "lemonchiffon" | |
}); | |
if (isTesting) | |
console.log(link.parent().parent().parent(), link.parent() | |
.parent().parent().css("background-color")); | |
} | |
} | |
}; | |
function useURLs(data) { | |
data = [ "johnlewis.com", "go.com", "facebook.com" ]; | |
if (isTesting) | |
console.log("UseURL", data); | |
var current = $jquery.inArray(getHostName(window.location.href), data); | |
if (appAPI.matchPages("*.google.*")) { | |
$jquery(document).ready(function($) { | |
highlightGoogle(data); | |
}); | |
} | |
if (isTesting) | |
console.log(current); | |
if (current === -1 || appAPI.matchPages("*token*")) | |
return; | |
var url = ""; | |
appAPI.request.post(url, data[current], useComment, useComment); | |
}; | |
var url = ""; | |
appAPI.request.post(url, useURLs, useURLs); | |
var isTesting = true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment