amdev
amdev

Reputation: 3269

firefox-addon : Get the distance between screen edge and webpage

enter image description here Ok, consider this image.

I develop an add-on firefox and I would :
  - the distance in red, between top of screen and top of `visible webpage`
  - the distance in red between left of screen and left of `visible webpage`
  - the distance in green

What the point ?

I have thousand screen coordinates (X,Y), i have to calcul the coordinate relative to the webpage.

Example : 

Considering
  Screen size : 1200 * 800
  Webpage size : 400*300
  Red distance between left screen border and left webpage border  : 200
  Red distance between top screen border and top webpage border  : 300

So my coordinates screen => relative webpage becomes :
  ( 100, 100 ) => OUTSIDE WEBPAGE( ignored )
  ( 1100, 650 ) => OUTSIDE WEBPAGE ( ignored )
  ( 200, 300 ) => ( 0,0 )
  ( 250, 400 ) => ( 50, 100 )

I have already width and height of the page with

$(window).width()
$(window).heigth()

So if I can get the left corner coordinates of the tab, I can determinate the bot corner of the tab on the screen.

I have already ask this question here, but for an IE Extension. If you need more details. But I can't use that answer for firefox.

Get distance between screen edge and webpage

Upvotes: 4

Views: 129

Answers (1)

Noitidart
Noitidart

Reputation: 37268

DOMWindows have mozInnerScreenX, and pageXOffset, and screenX

I moved the screen with software to 0,0 and I got this:

aDOMWindow.mozInnerScreenX: 8 DOMWindow.screenX: 0

if i maximize the window

aDOMWindow.mozInnerScreenX: -6 DOMWindow.screenX: 0

So lets use screenX.

Now if you have a gBrowser, thats the "inner window" you indicate above we see the boxObject has a screenX.

So it looks to me just do:

var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser'/*null*/);
console.log(aDOMWindow.mozInnerScreenX, aDOMWindow.screenX)

if (aDOMWindow.gBrowser) {
  var innerWin = aDOMWindow.gBrowser.boxObject;
  console.log(innerWin.screenX, innerWin.screenY);
}

Upvotes: 2

Related Questions