@@ -37,58 +37,94 @@ var Prompts = (() => {
3737 async open ( data ) {
3838 promptData = data ;
3939 this . close ( ) ;
40+
41+ let url = browser . runtime . getURL ( "ui/prompt.html" ) ;
4042 let { width, height, left, top, parent } = data . features ;
4143 let options = {
42- url : browser . runtime . getURL ( "ui/prompt.html" ) ,
44+ url,
4345 type : "popup" ,
44- width,
45- height,
46- } ;
46+ }
47+
48+ if ( ! parent ) {
49+ parent = await browser . windows . getCurrent ( ) ;
50+ }
51+
4752 if ( UA . isMozilla ) {
4853 options . allowScriptsToClose = true ;
4954 }
55+
5056 if ( ! ( "windows" in browser ) ) {
5157 // Android, most likely
52- this . currentTab = await browser . tabs . create ( { url : options . url } ) ;
58+ this . currentTab = await browser . tabs . create ( { url} ) ;
5359 return ;
5460 }
55- if ( ! parent ) parent = await browser . windows . getCurrent ( )
56- let popup = this . currentWindow = await browser . windows . create ( options ) ;
61+
62+ const centerOnParent = ( dim ) => {
63+ const { width, height } = dim ;
64+ dim . left =
65+ left === undefined
66+ ? Math . round ( parent . left + ( parent . width - width ) / 2 )
67+ : left ;
68+ dim . top =
69+ top === undefined
70+ ? Math . round ( parent . top + ( parent . height - height ) / 2 )
71+ : top ;
72+ return dim ;
73+ } ;
74+
75+ if ( width && height ) {
76+ let size = { width, height } ;
77+ url += `?size=${ JSON . stringify ( size ) } ` ;
78+ if ( parent ) {
79+ ( { left, top } = Object . assign ( options , centerOnParent ( size ) ) ) ;
80+ }
81+ }
82+ debug ( "Prompt pre-opening options" , options , left , top , width , height ) ; // DEV_ONLY
83+ let popup = ( this . currentWindow = await browser . windows . create ( options ) ) ;
5784
5885 if ( parent ) {
59- // center to the given parent window (default last focused browser tab)
60- if ( left === undefined ) left = Math . round ( parent . left + ( parent . width - popup . width ) / 2 ) ;
61- if ( top === undefined ) top = Math . round ( parent . top + ( parent . height - popup . height ) / 2 ) ;
86+ ( { left, top } = centerOnParent ( {
87+ width : width || popup . width ,
88+ height : height || popup . height ,
89+ } ) ) ;
6290 } else {
63- // features.parent explicitly nulled: use given left & top or default to auto-centering on main screen
64- if ( left === undefined ) ( { left} = popup ) ;
65- if ( top === undefined ) ( { top} = popup ) ;
91+ // use given left & top or default to auto-centering on main screen
92+ if ( left === undefined ) ( { left } = popup ) ;
93+ if ( top === undefined ) ( { top } = popup ) ;
6694 }
6795
68- // work around for letterboxing changes (https://bugzilla.mozilla.org/show_bug.cgi?id=1330882)
69- let { width : popupWidth , height : popupHeight } = popup ;
70- if ( width && height && ( popupWidth !== width || popupHeight !== height ) ) {
71- left += Math . round ( ( popupWidth - width ) / 2 ) ;
72- top += Math . round ( ( popupHeight - height ) / 2 ) ;
73- await browser . windows . update ( popup . id ,
74- { left, top, width, height, focused : false } ) ;
75- }
96+ debug ( "Prompt post-opening options" , popup , options , left , top , width , height ) ;
7697
77- for ( let attempts = 2 ; attempts -- > 0 ; ) {
78- // position gets set only 2nd time, moz bug?
79- await browser . windows . update ( popup . id ,
80- { left, top, focused : false } ) ;
81- }
82- if ( parent ) {
83- await browser . windows . update ( parent . id , { focused : true } ) ;
98+ // work around for resistFingerprinting new window rounding (https://bugzilla.mozilla.org/show_bug.cgi?id=1330882)
99+ if (
100+ width &&
101+ height &&
102+ ( popup . width !== width ||
103+ popup . height !== height ||
104+ popup . left !== left ||
105+ popup . top !== top )
106+ ) {
107+ popup = await browser . windows . update ( popup . id , {
108+ left,
109+ top,
110+ width,
111+ height,
112+ } ) ;
113+ for ( let attempts = 2 ; attempts -- > 0 ; ) {
114+ debug ( "Resizing" , popup , { left, top, width, height } ) ; // DEV_ONY
115+ popup = await browser . windows . update ( popup . id , { width, height } ) ;
116+ if ( popup . width == width || popup . height == height ) {
117+ break ;
118+ }
119+ }
84120 }
85121 }
122+
86123 async close ( ) {
87124 if ( this . currentWindow ) {
88125 try {
89126 await browser . windows . remove ( this . currentWindow . id ) ;
90127 } catch ( e ) {
91- debug ( e ) ;
92128 }
93129 this . currentWindow = null ;
94130 } else if ( this . currentTab ) {
0 commit comments