-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathextension.js
106 lines (89 loc) · 3.53 KB
/
extension.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(function() {
var PERMS_URL_PARAMS = ["perms","scope"],
PERMS_FORM_PARAM = "perms",
FORM_ID = "uiserver_form";
function split_url_parts() {
var query_parts = window.location.search.substring(1).split("&");
var dict = {};
for(var part_i in query_parts) {
var part = query_parts[part_i];
var key = decodeURIComponent(part.split("=")[0]);
var value = decodeURIComponent(part.split("=")[1]);
dict[key] = value;
}
return dict;
}
function get_active_permissions() {
var parts = split_url_parts();
var ret = {};
for(var i in PERMS_URL_PARAMS)
{
var param = PERMS_URL_PARAMS[i];
if(param in parts && parts[param] !== "")
{
var perms = parts[param].split(",");
for(var j in perms)
ret[perms[j]] = true;
}
}
console.debug("Facebook Parameters: ",ret);
return ret;
}
function set_perms_in_form(choosen) {
/*
* Modify the form live instead of reloading the page.
* Could be used in the future.
*/
var perms = choosen.join(",");
document.getElementById(FORM_ID)[PERMS_FORM_PARAM].value = perms;
}
function reload_page(choosen) {
var url = window.location.origin + window.location.pathname;
var parts = split_url_parts();
for(var i in PERMS_URL_PARAMS)
{
var param = PERMS_URL_PARAMS[i];
parts[param] = choosen.join(",");
}
console.log(parts);
var new_params = [];
var enc = encodeURIComponent;
for(key in parts) {
new_params.push((enc(key) + "=" + enc(parts[key])));
}
console.log(url + "?" + new_params.join("&"));
window.location.replace(url + "?" + new_params.join("&"));
}
function generate_header() {
/*
* FIXME: some kind of proper templating or something here.
* Maybe find a lightweight js lib for DOM manipulation.
*/
var permissions = get_active_permissions();
var holder = document.createElement('div');
holder.role = "toolbar";
holder.className = "fPrivacyHeader";
holder.innerHTML += '<a target="_new" href="https://www.facebook.com/settings?tab=applications">Application Settings</a>';
for(var perm in permissions) {
holder.innerHTML += '<div><input type="checkbox" id="'+perm+'" value="'+perm+'" checked=checked name="newperms" /><label for="'+perm+'">'+perm+'</label></div>';
}
if(Object.keys(permissions).length !== 0) {
var btn = document.createElement("button");
btn.appendChild(document.createTextNode("Update"));
btn.onclick = function() {
// "Update" is clicked: figure out what's checked and reload the window.
var elements = document.getElementsByName("newperms");
var permissions = Array.prototype.slice.call(elements);
permissions = permissions.filter(function(me){ return me.checked; });
permissions = permissions.map(function(me){ return me.value; });
reload_page(permissions);
};
var div = document.createElement("div");
div.appendChild(btn);
holder.appendChild(div);
}
// put it in the pizza!
document.body.insertBefore(holder, document.body.firstChild);
}
generate_header();
})();