Javascript bookmarklets variables are not scoped at Window #575

Closed
s2marine opened this Issue Jul 12, 2012 · 11 comments

3 participants

@s2marine

For example, this bookmarklet

javascript: var hello = 'world'

It works when I click it from the bookmark bar.
But when I use the b, variable is still undefined.

@philc
Owner

Stange (and minor), but our implementation should agree with Chrome's. Thanks for filing it!

@int3
Collaborator

Hmm, I think I know the reason why... Coffeescript automatically compiles everything in strict mode, and strict mode ensures that variables created in eval() do not escape into the surrounding scope.

@philc
Owner

Apparently the instapaper bookmarklet does not work via the "b" dialog anymore, and I suspect this might be related. If not we should file it as a separate issue, since that's a major bookmarklet that people use.

@philc philc was assigned Jul 22, 2012
@philc
Owner

The reason variables don't bleed into the page scope is the js sandbox Chrome provides for extension content scripts. It's unrelated to coffeescript.

To match chrome's behavior of scoping bookmarklet variables to the page, we have two options:

  • use window.location = ..., which does scope variables to the page's context
  • eval the bookmarklet in the page's context by embedding a <script> tag, as described here.
@int3
Collaborator

FWIW, instapaper seems to work for me now. I think doing eval fixed it, actually. Unless we can find a specific bookmarklet that is failing because of this behavior, I don't think we need to fix it.

@philc
Owner

Yep, I followed up on that instapaper bug report and the bookmarklet works for me as well, he was mistaken.

@int3
Collaborator

Nah I don't think he was mistaken -- it wasn't working till we started using eval in our last release.

@int3 int3 added a commit that closed this issue Nov 10, 2012
@int3 int3 Inject bookmarklets by appending them as scripts.
This *should* fix the permission problems that `eval` faced.

Closes #575, #710.
a3eb1cb
@int3 int3 closed this in a3eb1cb Nov 10, 2012
@ForSpareParts ForSpareParts pushed a commit to ForSpareParts/vimium that referenced this issue Apr 3, 2014
@int3 int3 Inject bookmarklets by appending them as scripts.
This *should* fix the permission problems that `eval` faced.

Closes #575, #710.
5f3e12b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment