From f9602fa5a7b34cc5412597db8b98afbc91e435c1 Mon Sep 17 00:00:00 2001 From: Deathamns Date: Sun, 19 Oct 2014 17:36:34 +0200 Subject: [PATCH] Implement popup autoresizing for Safari By default, Safari doesn't resize the popup to its content, but it's possible to set the size pragmatically. The popup will be resized every time when a change happens in the DOM tree. --- meta/safariextz/Info.plist | 4 ++++ src/Info.plist | 4 ++++ src/js/vapi-common.js | 33 ++++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/meta/safariextz/Info.plist b/meta/safariextz/Info.plist index 489b0599f..48d45c364 100644 --- a/meta/safariextz/Info.plist +++ b/meta/safariextz/Info.plist @@ -27,8 +27,12 @@ Filename popup.html + Height + 1 Identifier popover + Width + 1 Toolbar Items diff --git a/src/Info.plist b/src/Info.plist index 669678d8f..dfce939ac 100644 --- a/src/Info.plist +++ b/src/Info.plist @@ -27,8 +27,12 @@ Filename popup.html + Height + 1 Identifier popover + Width + 1 Toolbar Items diff --git a/src/js/vapi-common.js b/src/js/vapi-common.js index d70c4497a..e2153d59b 100644 --- a/src/js/vapi-common.js +++ b/src/js/vapi-common.js @@ -49,12 +49,35 @@ if (window.chrome) { // update popover size to its content if (safari.self.identifier === 'popover' && safari.self) { - vAPI.updatePopoverSize = function() { - safari.self.width = document.body.clientWidth; - safari.self.height = document.body.clientHeight; - }; + (function() { + var upadteTimer = null; + var resizePopover = function() { + if (upadteTimer) { + return; + } - setTimeout(vAPI.updatePopoverSize, 200); + upadteTimer = setTimeout(function() { + safari.self.width = document.body.clientWidth; + safari.self.height = document.body.clientHeight; + upadteTimer = null; + }, 50); + }; + + var mutObs = window.MutationObserver || window.WebkitMutationObserver; + + if (mutObs) { + (new mutObs(resizePopover)).observe(document, { + childList: true, + attributes: true, + characterData: true, + subtree: true + }); + } + else { + // Safari doesn't support DOMAttrModified + document.addEventListener('DOMSubtreeModified', resizePopover); + } + })(); } }