forked from mjpizz/reclaimprivacy
/
utils.js
129 lines (122 loc) · 4.61 KB
/
utils.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*!
* ReclaimPrivacy JavaScript Library
* http://www.reclaimprivacy.org/
*
* Copyright 2010, ReclaimPrivacy.org
* Licensed under the GPL Version 2 license
*/
var MAX_RETRIES_FOR_FRAMEWINDOW_DOCUMENT = 4;
var FRAME_JAVASCRIPT_LOAD_DELTA_IN_MILLISECONDS = 3000;
function debug() {
try {
if (opera && opera.postError) {
var string = "";
for (var i in arguments) {
string += arguments[i];
}
opera.postError(string);
} else {
window.console.debug.apply(console, arguments);
}
} catch(e) {}
}
function _extractUrlsFromPrivacySettingsPage(html, sectionName, responseHandler) {
// find 'basic' section URL
var privacyAnchors = jQuery('.fbPrivacyPage a', html);
privacyAnchors.each(function(){
var anchor = jQuery(this);
var href = anchor.attr ? anchor.attr('href') : anchor;
var regExp = new RegExp(".*section=" + sectionName + ".*");
if (regExp.test(href)){
debug("setting section for '" + sectionName + " (href=", href, ")");
urlForSection[sectionName] = href;
}
});
responseHandler(urlForSection[sectionName]);
}
var urlForSection = {};
function getUrlForV2Section(sectionName, responseHandler, options) {
if (urlForSection[sectionName]) {
// already cached, just respond with it
responseHandler(urlForSection[sectionName]);
} else {
if (options && options.test) {
_extractUrlsFromPrivacySettingsPage(options.test, sectionName, responseHandler);
} else {
// need to grab the signed URLs first (and cache them)
jQuery.ajax({
type: 'GET',
url: 'http://www.facebook.com/settings/?tab=privacy',
success: function(html){
_extractUrlsFromPrivacySettingsPage(html, sectionName, responseHandler);
},
error: function(){
debug("failed to load main privacy tab for analysis.");
responseHandler(false);
}
});
}
}
}
// helper that returns true if we are on facebook.com
function isOnFacebook() {
return /(facebook.com)/.test(document.location.toString());
}
// helper to deal with a Facebook page inside an iframe. The first parameter,
// 'source', can be either:
//
// * a string
// * a function
// * any other value
//
// In the first case, the string will be interpreted as a URL. That URL will be
// loaded in an iframe and the handler will be called with the documentElement
// of the given URL document. It's used for the real thing.
// In the second case, the handler will be called with the result of the
// given function (called without parameters).
// In the last case, the handler will be called with the value itself.
function withFramedPageOnFacebook(source, handler) {
if (typeof(source) == 'string') {
// Assume URL
if (isOnFacebook()) {
loadUrlInIframe(source, handler);
} else {
debug("cannot perform checks on a non-Facebook URL");
}
} else if (typeof(source) == 'function') {
handler(source());
} else {
handler(source);
}
}
function loadUrlInIframe(url, handler) {
debug("loading framed page ", url, "...");
var iframe = jQuery('<iframe/>');
jQuery('body').append(iframe);
jQuery(iframe).load(function(){
debug("framed page ", url, " loaded...");
var tries = 0;
var tryToCallHandlerAndRetryAfterWaiting = function(){
// ensure the document works
try {
debug("trying to handle loaded framed page ", url, "...");
var frameWindow = iframe[0].contentWindow;
handler(frameWindow.document.documentElement);
} catch(e) {
// failed, reschedule another try
tries += 1;
if (tries < MAX_RETRIES_FOR_FRAMEWINDOW_DOCUMENT) {
debug("failed to load page, retrying...");
setTimeout(tryToCallHandlerAndRetryAfterWaiting, FRAME_JAVASCRIPT_LOAD_DELTA_IN_MILLISECONDS);
} else {
debug("failed to load page, done retrying, just moving forward as a failsafe.");
var frameWindow = iframe[0].contentWindow;
handler(frameWindow.document.documentElement);
}
}
};
setTimeout(tryToCallHandlerAndRetryAfterWaiting, FRAME_JAVASCRIPT_LOAD_DELTA_IN_MILLISECONDS);
});
jQuery(iframe).attr('src', url);
jQuery(iframe).addClass('utility-frame');
}