XrayWrapper denies access to window.Intl.DateTimeFormat from a content script

Hi

I have a content script in an addon that should format dates.
It works fine in scratchpad using window.Intl.DateTimeFormat something like -
var date = new Date(Date.UTC(2012, 11, 20)); console.log(new Intl.DateTimeFormat().format(date));

When I test that code in my addon though, this error is thrown in the console -
"XrayWrapper denied access to property "DateTimeFormat" (reason: object is not safely Xrayable). See https://developer.mozilla.org/en-US/docs/Xray_vision"

From the details on the page about Xray vision, I can’t figure out how to get this to work.
Is there a different way to access objects like Date and Intl that should work from a content script?

I tried some things like gBrowser.contentWindow.date, Components.utils.waiveXrays and wrappedJSObject.

Thanks

I can’t reproduce this in Firefox release, Dev Edition and Nightly.

manifest.json:

{
“manifest_version”: 2,
“name”: “xray waiver”,
“version”: “1.0”,
“icons”: {
“48”: “icons/page-48.png”
},
“background”: {
“scripts”: [“background.js”]
},
“browser_action”: {
“default_icon”: “icons/page-32.png”
},
“permissions”: [“activeTab”]
}

background.js:

const contentScript =
var date = new Date(Date.UTC(2012, 11, 20)); console.log(new Intl.DateTimeFormat().format(date));

function inject() {
browser.tabs.executeScript({code: contentScript});
}

browser.browserAction.onClicked.addListener(inject);

…loaded into http://example.org, gives me “2012-12-19” in the console.

Is this the same setup you have?

I tried some things like gBrowser.contentWindow.date, Components.utils.waiveXrays and wrappedJSObject.

I think window.wrappedJSObject ought to get you an un-xrayed window. But I haven’t tried it here.

Thanks for your help.

I tried the code you provided above, but I got this error in the latest Developer Edition -
SyntaxError: expected expression, got keyword 'var'
(background.js line 2)

Was there something else missing there after the const…?

Because of that error though, I removed the first line in your background.js example and it did log the date to the console, which made me realise that my code was actually like -
new window.Intl.DateTimeFormat
and window is not from the right context.

Using just new Intl.DateTimeFormat does work fine. :relaxed:

I also tried using window.wrappedJSObject.Intl though; it fixed the previous error, but threw this one -
Permission denied to access property "weekday".

weekday is a property in the data format object I pass in for the date.

Oh! I was using backticks “`” for the value of contentScript, and apparently Discourse eats these unless you escape them :). So it should have been:

const contentScript =
`var date = new Date(Date.UTC(2012, 11, 20));
console.log(new Intl.DateTimeFormat().format(date));`;

function inject() {
browser.tabs.executeScript({code: contentScript});
}

browser.browserAction.onClicked.addListener(inject);

Using just new Intl.DateTimeFormat does work fine. :relaxed:

Awesome, so it sounds like you are good?