tab.contentWindow doesn't allow access to JS globals defined on the tab's page

RESOLVED WONTFIX

Status

Mozilla Labs
Jetpack Prototype
P3
normal
RESOLVED WONTFIX
9 years ago
8 years ago

People

(Reporter: matt, Assigned: atul)

Tracking

unspecified
x86
Linux

Details

(Reporter)

Description

9 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2a1pre) Gecko/20090524 Minefield/3.6a1pre (.NET CLR 3.5.30729)
Build Identifier: 

jetpack.tabs[i].contentWindow links to what looks like the correct object in firebug, but its behavior is incorrect.  Attempts to accessing variables defined in javascript in the window return undefined.

Reproducible: Always

Steps to Reproduce:
make a page:

<html>
<head><title>demo</title></head>
<body><p>doc</p><script type='text/javascript'>
  var myVar = 1;
</script>
</body>

open the page in the last tab of firefox.  then run the following jetpack script:

var lastTab = jetpack.tabs[jetpack.tabs.length-1];
var lastTab = jetpack.tabs[jetpack.tabs.length-1];
console.log( "lastTab", 
	lastTab.contentWindow, 
	lastTab.contentWindow.myVar ,
	lastTab.contentWindow.content, 
	lastTab.contentWindow.content.myVar,
	lastTab.contentWindow.self, 
	lastTab.contentWindow.self.myVar);

Actual Results:  
the attempts to access .mv return undefined.
lastTab.contentWindow.content points to the current tab.

Expected Results:  
myVar should be 2.
lastTab.contentWindow.content should be the contents of contentWindow, not the current window.

Comment 1

9 years ago
Probably you need to use "wrappedJSObject".

console.log( "lastTab", 
    lastTab.contentWindow, 
    lastTab.contentWindow.wrappedJSObject.myVar ,
    lastTab.contentWindow.wrappedJSObject.content, 
    lastTab.contentWindow.wrappedJSObject.content.myVar,
    lastTab.contentWindow.wrappedJSObject.self, 
    lastTab.contentWindow.wrappedJSObject.self.myVar);

@see
wrappedJSObject - MDC
https://developer.mozilla.org/en/WrappedJSObject
(Assignee)

Comment 2

9 years ago
Thanks for the bug report!  As Makoto mentioned, this has to do with the fact that the content window is actually a "safe" version of the window's content that behaves in an expected way, but its downside is that it can't access custom JS variables.  GreaseMonkey makes the original window available through an "unsafeWindow" property:

  http://wiki.greasespot.net/UnsafeWindow

We'll probably be using a similar name, so that e.g. you can get to your JS functions by accessing jetpack.tabs.focused.unsafeContentWindow.  The reason I'd like to do this instead of telling Jetpack developers to use "wrappedJSObject" is probably the same for why GreaseMonkey decided not to: the name "wrappedJSObject" isn't very meaningful to audiences who don't know what XPConnect is, and it also exposes something in the underlying platform that could change at some point.
Status: UNCONFIRMED → NEW
Ever confirmed: true
(Assignee)

Comment 3

9 years ago
Renamed the bug title to better reflect the root problem.
Summary: jetpack.tabs[i].contentWindow is part i'th tab / part current tab → tab.contentWindow doesn't allow access to JS globals defined on the tab's page

Updated

9 years ago
Assignee: nobody → avarma
Priority: -- → P3
Target Milestone: -- → 0.5
(Assignee)

Comment 4

8 years ago
I've talked to mrbkap about this and we've agreed that friendly-yet-secure behavior is to modify XPCNativeWrappers so that they see if the property on the underlying object exists and, if so, throw an exception that says something like:

  "The property 'foo' exists on the object but can't be accessed for security reasons. If this is indeed the property you'd like to access, you can retrieve it by first accessing this object's 'wrappedJSObject' property. Please note that it may not behave as you expect if the web page you're accessing is coded in an unusual way."

To prototype this behavior and see how well it works without waiting for another release of Firefox, we can approximate the same kind of behavior using a FlexibleWrapper.

Updated

8 years ago
Target Milestone: 0.5 → 0.6
We will be monitoring all these issues after the rebooted Jetpack code base is released in the first week of March to ensure their causes are not duplicated. Many of the bugs/issues with the prototype version of Jetpack will be made irrelevant given the structure of the new SDK.
Status: NEW → RESOLVED
Last Resolved: 8 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.