// ==UserScript==
// @name SocialGraphAPIValidator
// @namespace api.kurrik@google.com
// @description Validates the current page against the social graph API
// @include *
// ==/UserScript==
function bind(func, obj) {
return function() {
func.apply(obj, arguments);
};
};
var sg = function() {
};
sg.prototype = {
dom_html : null,
out_window : null,
encodePost : function(data) {
var key = null,
out = [],
tempout = [];
for (key in data) {
if (data.hasOwnProperty(key)) {
tempout = [];
tempout.push(encodeURIComponent(key));
tempout.push(encodeURIComponent(data[key]));
out.push(tempout.join("="));
}
}
return out.join("&");
},
makeOutWindow : function() {
this.out_window = window.open("", "", "scrollbars=yes,resizable=yes,toolbar=no,width=400,height=300");
this.out_window.document.write("
Social Graph API Validation Results
");
},
print : function(msg) {
GM_log(msg);
if (!this.out_window) {
alert("Please disable your popup blocker");
} else {
this.out_window.document.write(msg);
this.out_window.document.close();
}
},
request : function() {
var metas = document.getElementsByTagName("meta"),
meta = null,
content_type = "text/html; charset=utf-8",
body_text = "",
data = null,
i = 0;
this.makeOutWindow();
this.dom_html = document.getElementsByTagName("html")[0] ||
document.getElementsByTagName("RDF")[0];
if (!this.dom_html) {
this.print("Could not find HTML or RDF data", true);
}
for (i = 0; meta = metas[i]; i++){
if (meta.getAttribute("http-equiv") &&
meta.getAttribute("http-equiv").toLowerCase() == "content-type") {
if (meta.getAttribute("content")) {
content_type = meta.getAttribute("content");
}
}
}
body_text = this.dom_html.innerHTML;
if (!body_text) {
body_text = [ "\n",
new XMLSerializer().serializeToString(this.dom_html) ].join("");
content_type = "application/rdf+xml; charset=utf-8";
}
data = {
"url" : window.location.href,
"body" : body_text,
"contentType" : content_type,
"urlFormat" : "base"
};
if (console && console.log) {
console.log("Validating", data);
}
GM_xmlhttpRequest({
method: "POST",
url: "http://socialgraph.apis.google.com/testparse",
headers: {
"Content-type" : "application/x-www-form-urlencoded",
"User-agent": "Mozilla/4.0 (compatible) Greasemonkey"
},
data : this.encodePost(data),
onload: bind(this.response, this),
onerror: bind(this.errorHappened, this)
});
},
response : function(data) {
var msg = data.responseText.replace(/ /g, " ").replace(/\n/g, "
");
if (data.status == "200") {
this.print(msg, true);
} else {
this.print([ "Problem fetching data! Response code = ", data.status ].join(""), true);
}
},
errorHappened : function(data) {
this.print([ "Problem fetching data! Response code = ",
data.status,
" status text = ",
data.statusText ].join(""), true);
}
}
var instance = new sg();
GM_registerMenuCommand("Validate on Social Graph API", bind(instance.request, instance));