forked from sinamt/SGR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiframe.js
132 lines (104 loc) · 3.69 KB
/
iframe.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
130
131
(function($) {
//console.log('in iframe.js');
// If the iframe preloader did not find an iframe, return immediately
//
if ($(".sgr_iframe").length <= 0) {
//debug("no sgr_iframe exists for " + self.location.href);
return;
}
//debug("** sgr_iframe exists for " + self.location.href);
// If this is the google reader settings iframe, return
//
if (self.location.href.match(/\/\/(www\.|)google\.com\/reader\/settings/)) {
//debug("in greader settings iframe, returning");
return;
}
// Minimum allowed iframe height to be sent to the parent
//
const MIN_IFRAME_HEIGHT = 700;
// Maximum allowed times to send height to parent.
//
const MAX_SEND_SIZE = 25;
// Counter for amount of times height has been sent to parent.
var send_size_counter = 0;
// Execute the posting of a message to our parent window.
//
function sendMessageToParent(msg) {
if (chrome) {
var iframe_id = document.getElementsByClassName("sgr_iframe")[0].innerText;
if (iframe_id) {
chrome.extension.sendRequest({action: "window_height", window_height: msg, iframe_id: iframe_id}, function(response) {
//debug("iframe.js: " + response.action + " - " + response._msg);
});
}
}
}
// Find the height of the window and send it to the parent window.
// This is done via parent.postMessage()
//
function sendSizeToParent() {
//debug('sendSizeToParent()');
// Check if we have exceeded the maximum allowed times to send the height to the parent.
// This is here as a catchall or precaution in case we get stuck in a resize loop and continually
// spam the parent with height values. This can sometimes occur if we use a window resize event
// handler to determine when to send height to the parent.
//
if (send_size_counter > MAX_SEND_SIZE) {
//debug("sendSizeToParent() exceeded call limit");
if (jQuery) {
$(window).unbind('resize', sendSizeToParent);
}
return false;
}
try {
var scroll_height = document.body.scrollHeight;
} catch(e) {
debug("document.body.scrollHeight not found");
return false;
}
send_size_counter += 1;
var height;
// If jQuery has loaded, use it to find the window height
//
if (jQuery) {
// document.body.scrollHeight *seems* to be the correct height to use in
// cases where a page has been shortened in height during it's load process.
// So if document.body.scrollHeight is less than $(document).height(), but
// still larger than the minimum allowable iframe height, use it.
//
if (scroll_height < $(document).height() && scroll_height > MIN_IFRAME_HEIGHT) {
// Add a little fudge (20px) to cover fringe cases of incorrect height
//
height = scroll_height + 20;
} else {
height = $(document).height();
}
// If we don't have access to jQuery, simply use document.body.scrollHeight
//
} else {
// Add a little fudge (20px) to cover fringe cases of incorrect height
//
height = scroll_height + 20;
}
// Send the height to the parent using postMessage()
//
sendMessageToParent(height);
return false;
}
// MAIN
//
// Send the window size to the parent right now
//
//debug("immediate sendSizeTOParent " + self.location.href);
sendSizeToParent();
$(document).ready(function() {
//debug("document.ready sendSizeTOParent " + self.location.href);
sendSizeToParent();
});
// Send the window size to the parent after the window has loaded
//
$(window).load(function() {
//debug("window.load sendSizeTOParent " + self.location.href);
sendSizeToParent();
});
})(jQuery);