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