forked from kevinburke/customize-twitter-1.1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customize-twitter-1.1.js
70 lines (61 loc) · 1.98 KB
/
customize-twitter-1.1.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
var CustomizeTwitterWidget = function(data) {
var notNumeric = function(n) {
return isNaN(parseFloat(n)) && isFinite(n);
};
var createCssElement = function(doc, url) {
var link = doc.createElement("link");
link.href = url;
link.rel = "stylesheet";
link.type = "text/css";
return link;
};
var embedCss = function(doc, url) {
var link = createCssElement(doc, url);
var head = doc.getElementsByTagName("head")[0];
head.appendChild(link);
};
var contains = function(haystack, needle) {
return haystack.indexOf(needle) >= 0;
};
var isTwitterFrame = function(frame) {
return frame.frameElement.id.indexOf('twitter-widget') >= 0;
}
/**
* The main event loop - calls itself if we haven't found all of the frames
* yet.
*/
var evaluate = function(framesWithStyles, widgetCount, timeoutLength) {
for (var i = 0; i < frames.length; i++) {
if (isTwitterFrame(frames[i]) &&
!contains(framesWithStyles, frames[i].name)
) {
embedCss(frames[i].document, data.url);
framesWithStyles.push(i);
}
}
if (framesWithStyles.length < widgetCount) {
setTimeout(function() {
evaluate(framesWithStyles);
}, timeoutLength);
}
}
if (data.url === undefined) {
console.log("need to specify a link to your CSS file. quitting");
return;
}
var widgetCount;
if (data.widget_count === undefined || notNumeric(data.widget_count)) {
widgetCount = 1;
} else {
widgetCount = data.widget_count;
}
var timeoutLength;
if (data.timeout_length === undefined || notNumeric(data.timeout_length)) {
timeoutLength = 300;
} else {
timeoutLength = data.timeout_length;
}
setTimeout(function() {
evaluate([], widgetCount, timeoutLength);
}, timeoutLength);
}