OLD | NEW |
1 (function() | 1 (function() |
2 { | 2 { |
3 // Safari will load the popover once, and then show it everytime the icon is | 3 // Safari will load the popover once, and then show it everytime the icon is |
4 // clicked. While Chrome loads it everytime you click the icon. So in order to | 4 // clicked. While Chrome loads it everytime you click the icon. So in order to |
5 // make the popover show the right state and details we have to reload it | 5 // make the popover show the right state and details we have to reload it |
6 // everytime it is shown for a different tab. Also we have to reload the | 6 // everytime it is shown for a different tab. Also we have to reload the |
7 // popover when the background page wasn't ready yet, since we have to access | 7 // popover when the background page wasn't ready yet, since we have to access |
8 // the background page in the popover. | 8 // the background page in the popover. |
9 var backgroundPage = safari.extension.globalPage.contentWindow; | 9 var backgroundPage = safari.extension.globalPage.contentWindow; |
10 var readyState = backgroundPage.document.readyState; | 10 var readyState = backgroundPage.document.readyState; |
11 var activeTab = safari.application.activeBrowserWindow.activeTab; | 11 var activeTab = safari.application.activeBrowserWindow.activeTab; |
| 12 var stopResizing = function() {}; |
12 | 13 |
13 safari.self.addEventListener("popover", function() | 14 safari.self.addEventListener("popover", function() |
14 { | 15 { |
15 if (activeTab != safari.application.activeBrowserWindow.activeTab || readySt
ate != "complete") | 16 if (activeTab != safari.application.activeBrowserWindow.activeTab || readySt
ate != "complete") |
16 { | 17 { |
| 18 stopResizing(); |
17 document.documentElement.style.display = "none"; | 19 document.documentElement.style.display = "none"; |
18 document.location.reload(); | 20 document.location.reload(); |
19 } | 21 } |
20 }); | 22 }); |
21 | 23 |
22 | 24 |
23 // Safari doesn't adjust the size of the popover automatically to the size | 25 // Safari doesn't adjust the size of the popover automatically to the size |
24 // of its content, like when the ad counter is expanded/collapsed. So we add | 26 // of its content, like when the ad counter is expanded/collapsed. So we add |
25 // event listeners to do so. | 27 // event listeners to do so. |
26 var updateSize = function() | 28 var updateSize = function() |
27 { | 29 { |
28 safari.self.width = document.body.offsetWidth; | 30 safari.self.width = document.body.offsetWidth; |
29 safari.self.height = document.body.offsetHeight; | 31 safari.self.height = document.body.offsetHeight; |
30 }; | 32 }; |
31 | 33 |
32 window.addEventListener("load", function() | 34 window.addEventListener("load", function() |
33 { | 35 { |
34 updateSize(); | 36 updateSize(); |
35 | 37 |
36 var MutationObserver = window.MutationObserver || window.WebKitMutationObser
ver; | 38 var MutationObserver = window.MutationObserver || window.WebKitMutationObser
ver; |
37 if (MutationObserver) | 39 if (MutationObserver) |
38 { | 40 { |
39 new MutationObserver(updateSize).observe(document, { | 41 var mo = new MutationObserver(updateSize); |
| 42 mo.observe(document, { |
40 childList: true, attributes: true, | 43 childList: true, attributes: true, |
41 characterData: true, subtree: true | 44 characterData: true, subtree: true |
42 }); | 45 }); |
| 46 |
| 47 stopResizing = function() |
| 48 { |
| 49 mo.disconnect(); |
| 50 }; |
43 } | 51 } |
44 else | 52 else |
| 53 { |
45 document.addEventListener("DOMSubtreeModified", updateSize); | 54 document.addEventListener("DOMSubtreeModified", updateSize); |
| 55 |
| 56 stopResizing = function() |
| 57 { |
| 58 document.removeEventListener("DOMSubtreeModified", updateSize); |
| 59 }; |
| 60 } |
46 }); | 61 }); |
47 | 62 |
48 | 63 |
49 // Safari doesn't hide popovers automatically, when we change the active tab | 64 // Safari doesn't hide popovers automatically, when we change the active tab |
50 // programmatically, like when the options link is clicked. So we add an event | 65 // programmatically, like when the options link is clicked. So we add an event |
51 // listener to do so. | 66 // listener to do so. |
52 safari.application.addEventListener("activate", function() | 67 safari.application.addEventListener("activate", function() |
53 { | 68 { |
54 safari.self.hide(); | 69 safari.self.hide(); |
55 }, true); | 70 }, true); |
56 | 71 |
57 | 72 |
58 // import ext into the javascript context of the popover. This code might fail
, | 73 // import ext into the javascript context of the popover. This code might fail
, |
59 // when the background page isn't ready yet. So it is important to put it belo
w | 74 // when the background page isn't ready yet. So it is important to put it belo
w |
60 // the reloading code above. | 75 // the reloading code above. |
61 window.ext = { | 76 window.ext = { |
62 __proto__: backgroundPage.ext, | 77 __proto__: backgroundPage.ext, |
63 closePopup: function() | 78 closePopup: function() |
64 { | 79 { |
65 safari.self.hide(); | 80 safari.self.hide(); |
66 } | 81 } |
67 }; | 82 }; |
68 window.TabMap = backgroundPage.TabMap; | 83 window.TabMap = backgroundPage.TabMap; |
69 })(); | 84 })(); |
OLD | NEW |