Add property to provide JSON set of global variables for initial state of content scripts

RESOLVED FIXED in 1.8

Status

Add-on SDK
General
P3
enhancement
RESOLVED FIXED
6 years ago
5 years ago

People

(Reporter: Louis-Rémi BABE, Unassigned)

Tracking

unspecified

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

6 years ago
Attaching a contentScript to a tab is like executing a function.
I'd like to "call" the content scripts and pass it arbitrary (jsonable) values:

// in main.js
tabs.activeTab.attach({
  contentScriptFile: "cs.js",
  data: {a: 1, b: 2}
});

// in cs.js
self.data.a === 1;
self.data.b === 2;

currently, I have to wrap all my cs.js code in a port listener to achieve the same effect, which is not ideal:

// in main.js
var worker = tabs.activeTab.attach({
  contentScriptFile: "cs.js"
});
worker.port.emit( "init", {a: 1, b: 2} );

// in cs.js
self.port.on( "init", function( data ) {
  data.a === 1;
  data.b === 2;
});

Updated

6 years ago
Whiteboard: [triage:followup]
Assignee: nobody → myk
Whiteboard: [triage:followup]
I'm not sure I would use the "function" metaphor to describe such functionality, but I do see the value of providing a set of values to a content script to initialize its state.  I would just call the option something other than `data`, such as `globals`, and then define a global variable in the content script for each property of the JSON object to which `globals` is set, i.e.:

// in main.js
tabs.activeTab.attach({
  contentScriptFile: "cs.js",
  globals: { a: 1, b: 2 }
});

// in cs.js
// a === 1
// b === 2


To create a `data` global that encapsulated several values, you would do:

// in main.js
tabs.activeTab.attach({
  contentScriptFile: "cs.js",
  globals: { data: { a: 1, b: 2 } }
});
Assignee: myk → nobody
OS: Linux → All
Hardware: x86 → All

Updated

6 years ago
Severity: normal → enhancement
Priority: -- → P3
Summary: It should be possible to "call" contentscripts with arguments → Add property to provide JSON set of global variables for initial state of content scripts
Note: the property should be called contentScriptGlobals for consistency with the other content script properties.
(Reporter)

Comment 3

6 years ago
I sent a PR for this bug: https://github.com/mozilla/addon-sdk/pull/351

Comment 4

5 years ago
Commits pushed to master at https://github.com/mozilla/addon-sdk

https://github.com/mozilla/addon-sdk/commit/f82e7c8a8f520075d198ac77479e4a225a788faf
Bug 688127: Add property to provide JSON set of global variables for initial state of content scripts

https://github.com/mozilla/addon-sdk/commit/0006f26a99b3b9016ac8def642e125de31b87492
Merge pull request #411 from ochameau/fix688127

Bug 688127: Add property to provide JSON set of global variables for initial state of content scripts r=@ochameau
louisremi, I ended up rebased your patch on landed it so that it can go in next release!
Target Milestone: --- → 1.8
Thanks Louis rémi for this great contribution!
Status: NEW → RESOLVED
Last Resolved: 5 years ago
Resolution: --- → FIXED
Should we consider cherry-picking this for 1.7?

Comment 8

5 years ago
Commits pushed to stabilization at https://github.com/mozilla/addon-sdk

https://github.com/mozilla/addon-sdk/commit/f82e7c8a8f520075d198ac77479e4a225a788faf
Bug 688127: Add property to provide JSON set of global variables for initial state of content scripts

https://github.com/mozilla/addon-sdk/commit/0006f26a99b3b9016ac8def642e125de31b87492
Merge pull request #411 from ochameau/fix688127

Comment 9

5 years ago
Commits pushed to release at https://github.com/mozilla/addon-sdk

https://github.com/mozilla/addon-sdk/commit/f82e7c8a8f520075d198ac77479e4a225a788faf
Bug 688127: Add property to provide JSON set of global variables for initial state of content scripts

https://github.com/mozilla/addon-sdk/commit/0006f26a99b3b9016ac8def642e125de31b87492
Merge pull request #411 from ochameau/fix688127
You need to log in before you can comment on or make changes to this bug.