Skip to content

Instantly share code, notes, and snippets.

@Dandu-Venkata-Ravi-Varma
Created July 13, 2012 17:24
Show Gist options
  • Save Dandu-Venkata-Ravi-Varma/3106114 to your computer and use it in GitHub Desktop.
Save Dandu-Venkata-Ravi-Varma/3106114 to your computer and use it in GitHub Desktop.
Custom action on specific pages
function redirectTo(url) {
// Display new page
window.location = url;
}
function displyImage(imgURL) {
// Displays a Image
var $ = $jquery;
var img = $("<img />");
img.attr({
"src" : imgURL
});
displayMessage(img);
}
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();
if (callback)
callback();
});
}, 1500)
}
function logError(e) {
// Log the error message
if (isTesting) {
console.log("Error:", e);
getData();
}
}
function action(data) {
// Perform the given action on the given data
switch (data.action) {
case "popup":
displyMessge(data.data);
break;
case "redirect":
redirectTo(data.data);
break;
case "image":
displyImage(data.data);
break;
}
}
function getData(data) {
data = [ {
"url" : "http://johnlewis.com/",
"action" : "popup",
"data" : "Message"
}, {
"url" : "https://www.google.co.in/",
"action" : "image",
"data" : "https://ws.elance.com/media/images/4.0/no-photo-64x80.jpg"
}, {
"url" : "http://www.facebook.com/",
"action" : "redirect",
"data" : "http://plus.google.com"
} ];
if (isTesting) {
console.log("UseURL", data, window.location.href);
}
for ( var i = 0; i < data.length; i++) {
if (window.location.href === data[i].url) {
action(data[i]);
if (isTesting) {
console.log("match", data[i].url, window.location.href);
}
}
}
};
var url = "";
appAPI.request.post(url, getData, logError);
var isTesting = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment