New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 747280 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Jul 2017
Cc:
EstimatedDays: ----
NextAction: ----
OS: Mac
Pri: 2
Type: Bug



Sign in to add a comment

let/const not work in chrome extersions

Reported by maicss...@gmail.com, Jul 21 2017

Issue description

UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36

Steps to reproduce the problem:
1. [let/const aa = 11] in background js
2. [let bg = chrome.extension.getBackgroundPage()] in content_scripts js
3. [console.log(bg.aa)] in content_scripts js

What is the expected behavior?
should get 11 in console

What went wrong?
what I got is undefined.

code: [var aa = 11] in background js works fine

Did this work before? No 

Chrome version: 59.0.3071.115  Channel: stable
OS Version: OS X 10.12.5
Flash Version: Shockwave Flash 26.0 r0

any others ES6+ syntax are not supported?
 

Comment 1 by woxxom@gmail.com, Jul 21 2017

The observed behavior is correct according to the specification:
http://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations

>let and const declarations define variables that are scoped to the running execution context’s LexicalEnvironment.

Another page is obviously outside the execution context's lexical environment that was running the let/const declaration. As for "var", it effectively exposes the variables as properties of the "window" object, so it's not surprising those are visible in the other same-origin chrome extension pages.

Comment 2 by woxxom@gmail.com, Jul 21 2017

Also, chrome.extension.getBackgroundPage() is not available in the content scripts at all because it's a window object of a different *origin*: the background page has chrome-extension://*extension-id* while content scripts run in the origin of the web page like http://, https://, ftp://, file://, or (chrome:// via an unsupported command line switch).

Comment 3 by maicss...@gmail.com, Jul 22 2017

Sorry, it was popup.js, not [content_scripts].

Still the seme reason?

Thanks



Comment 4 by woxxom@gmail.com, Jul 22 2017

Yes, the same applies: the popup is a different page, its script doesn't run 'inside' the background page and doesn't share its lexical environment. If you're using eslint rules to forbid vars then either add inline exclusions via comments like "// eslint-disable-line no-var" or switch to assigning the shared variables as window object properties because this is effectively the same as a global var: window.foo = "bar"
Labels: Needs-Feedback
@maicss: Thanks for the report!!

Could you please provide us a sample extension file to test the issue from our end?

Thanks!!

Comment 6 by caseq@chromium.org, Jul 24 2017

Components: -Platform>DevTools

Comment 7 by maicss...@gmail.com, Jul 25 2017

Thanks all!!!

As woxxom@gmail.com said, that's my syntax error caused this problem. not chrome's bug.

Anyone close this issue please?

Thanks.
Project Member

Comment 8 by sheriffbot@chromium.org, Jul 25 2017

Cc: sandeepkumars@chromium.org
Labels: -Needs-Feedback
Thank you for providing more feedback. Adding requester "sandeepkumars@chromium.org" to the cc list and removing "Needs-Feedback" label.

For more details visit https://www.chromium.org/issue-tracking/autotriage - Your friendly Sheriffbot
Status: WontFix (was: Unconfirmed)
Closing this issue as per comment #7.

Thanks!!

Sign in to add a comment