public
Last active

an example Jetpack script

  • Download Gist
main.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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();
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.