OLD | NEW |
| (Empty) |
1 (function() | |
2 { | |
3 // Safari doesn't adjust the size of the popover automatically to the size | |
4 // of its content, like when the ad counter is expanded/collapsed. So we add | |
5 // event listeners to do so. | |
6 var mayResize = true; | |
7 var resizingScheduled = false; | |
8 | |
9 var updateSize = function() | |
10 { | |
11 if (mayResize && !resizingScheduled) | |
12 { | |
13 setTimeout(function() | |
14 { | |
15 safari.self.width = document.body.scrollWidth; | |
16 safari.self.height = document.body.offsetHeight; | |
17 | |
18 resizingScheduled = false; | |
19 }, 0); | |
20 | |
21 resizingScheduled = true; | |
22 } | |
23 }; | |
24 | |
25 window.addEventListener("load", function() | |
26 { | |
27 updateSize(); | |
28 | |
29 var MutationObserver = window.MutationObserver || window.WebKitMutationObser
ver; | |
30 if (MutationObserver) | |
31 { | |
32 new MutationObserver(updateSize).observe(document, { | |
33 childList: true, attributes: true, | |
34 characterData: true, subtree: true | |
35 }); | |
36 } | |
37 else | |
38 document.addEventListener("DOMSubtreeModified", updateSize); | |
39 }); | |
40 | |
41 // when using "white-space: nowrap", the overflown text overlaps the padding | |
42 // and neither clientWidth nor scrollWidth, we rely on when adjusting the size | |
43 // of the popover, inlcudes the overlapped area. So we have to use additional | |
44 // placeholders, in order to preserve padding. Since the dimensions of the | |
45 // popover are automatically correctly adjusted on Chrome, those placeholders | |
46 // would add extra empty space and therefore must only be rendered on Safari. | |
47 var style = document.createElement("style"); | |
48 style.textContent = ".safari-inline-block { display: inline-block; }"; | |
49 document.head.appendChild(style); | |
50 | |
51 | |
52 // Safari will load the popover once, and then show it everytime the icon is | |
53 // clicked. While Chrome loads it everytime you click the icon. So in order to | |
54 // make the popover show the right state and details, we have to emulate the | |
55 // same behavior as on Chrome, by reloading the popover every time it is shown
. | |
56 safari.self.addEventListener("popover", function() | |
57 { | |
58 mayResize = false; | |
59 document.documentElement.style.display = "none"; | |
60 window.location.reload(); | |
61 }); | |
62 | |
63 | |
64 // Safari doesn't hide popovers automatically, when we change the active tab | |
65 // programmatically, like when the options link is clicked. So we add an event | |
66 // listener to do so. | |
67 safari.application.addEventListener("activate", function() | |
68 { | |
69 safari.self.hide(); | |
70 }, true); | |
71 | |
72 | |
73 // import ext into the javascript context of the popover. This code might fail
, | |
74 // when the background page isn't ready yet. So it is important to put it belo
w | |
75 // the reloading code above. | |
76 var backgroundPage = safari.extension.globalPage.contentWindow; | |
77 window.ext = Object.create(backgroundPage.ext); | |
78 | |
79 ext.closePopup = function() | |
80 { | |
81 safari.self.hide(); | |
82 }; | |
83 | |
84 ext.backgroundPage = { | |
85 getWindow: function() | |
86 { | |
87 return backgroundPage; | |
88 }, | |
89 | |
90 // On Safari, you can't send messages from the popup to the | |
91 // background page. So we call the message listeners directly. | |
92 sendMessage: function(message, responseCallback) | |
93 { | |
94 if (!responseCallback) | |
95 responseCallback = function () {}; | |
96 | |
97 backgroundPage.ext.onMessage._dispatch(message, {}, responseCallback); | |
98 } | |
99 }; | |
100 | |
101 })(); | |
OLD | NEW |