Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created July 31, 2010 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/502276 to your computer and use it in GitHub Desktop.
Save hubgit/502276 to your computer and use it in GitHub Desktop.
an example Jetpack script
var tabs = require("tabs");
const Request = require("request").Request;
var include_pattern = new RegExp("http://(\w|-)+\.facebook\.com/friends/.*");
var url_server = "http://example.com/friendstore/";
tabs.onReady.add(function(tab) {
if (!tab.location.href.match(include_pattern))
return false;
var doc = tab.contentDocument;
var button = doc.createElement("input");
button.setAttribute("type", "submit");
button.setAttribute("value", "store friends");
var form = doc.createElement("form");
form.appendChild(button);
form.addEventListener("submit", function(e){
var nodes = doc.querySelectorAll("#ffriends .ffriend");
if (!nodes.length)
return false;
var friends = [];
for (var i = 0; i < nodes.length; i++)
friends.push(nodes[i].getAttribute("id").replace(/^f/, ""));
var total = doc.getElementById("summary").textContent.match(/ has (\d+) /)[1];
var profileName = doc.querySelectorAll("title").item(0).textContent.match(/Facebook \| (.+)/)[1];
var id = doc.location.href.match(/id=(\d+)/)[1];
var data = [];
data.push("id=" + encodeURIComponent(id));
data.push("friends=" + encodeURIComponent(friends.join(",")));
data.push("total=" + encodeURIComponent(total));
data.push("name=" + encodeURIComponent(profileName));
data = data.join("&");
new Request({
url: url_server,
content: data,
contentType: "application/x-www-form-urlencoded",
onComplete: function () {
if (this.response.status == "200")
doc.location.href = this.response.json.url;
}
}).post();
return false;
}, false);
var filters = doc.getElementById("friend_filters");
if (filters.parentNode)
filters.parentNode.insertBefore(form, filters);
else
new Request({
url: url_server,
onComplete: function () {
if (this.response.status == "200")
doc.location.href = this.response.json.url;
}
}).get();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment