Open Bug 1750430 Opened 4 years ago Updated 4 years ago

userScriptOptions should allow 'undefined' value

Categories

(WebExtensions :: General, defect, P5)

defect

Tracking

(Not tracked)

People

(Reporter: erosman, Unassigned)

References

Details

Setting undefined as a value causes error.

STR

const options = {
  a: true
};

const scriptObj = await browser.contentScripts.register({
  matches: ["*://*.org/*"],
  js: [{file: "/content_scripts/example.js"}],
  scriptMetadata: {
    a: options.a,
    b: options.b      // undefined
  }
});

Error

Type error for parameter userScriptOptions (Error processing scriptMetadata: Value 
must either: be a null value, be a number value, be a string value, be a boolean, 
be an array value, .name must () => {
          let array = Array.from(choices, forceString);
          let n = array.length - 1;
          array[n] = `or ${array[n]}`;

          return `must either [${array.join(", ")}]`;
        }, .resource must () => {
          let array = Array.from(choices, forceString);
          let n = array.length - 1;
          array[n] = `or ${array[n]}`;

          return `must either [${array.join(", ")}]`;
        }, .storage must () => {
          let array = Array.from(choices, forceString);
          let n = array.length - 1;
          array[n] = `or ${array[n]}`;

          return `must either [${array.join(", ")}]`;
        }, .injectInto must () => {
          let array = Array.from(choices, forceString);
          let n = array.length - 1;
          array[n] = `or ${array[n]}`;

          return `must either [${array.join(", ")}]`;
        }, or .disableSyncGM must () => {
          let array = Array.from(choices, forceString);
          let n = array.length - 1;
          array[n] = `or ${array[n]}`;

          return `must either [${array.join(", ")}]`;
        }) for userScripts.register.

Suggestions

  • undefined should be allowed as a value
  • The error notice should also list object as an allowed value
  • The error without the JS code would be easier to read

Side note: the very verbose weird-looking error message is a known bug: bug 1689733.

See Also: → 1689733

So this is because scriptMetadata is defined as PlainJSONValue which is itself defined like so:
https://searchfox.org/mozilla-central/rev/afbff3f3ae/toolkit/components/extensions/schemas/extension_types.json#136

The reason is that undefined values can't actually be define as JSON:

JSON.stringify(undefine) === undefined     // not a string!
JSON.stringify({a: undefined}) === "{}"    // an empty object.

I guess we could update our schemas to include allow undefined for array and property values, but I don't even believe our Schemas.jsm allows for an undefined base type.

Also, we probably worth aligning the new scripting API args type, since it should use JSON under the hood as well:
https://searchfox.org/mozilla-central/rev/afbff3f3ae/toolkit/components/extensions/schemas/scripting.json#34

The severity field is not set for this bug.
:mixedpuppy, could you have a look please?

For more information, please visit auto_nag documentation.

Flags: needinfo?(mixedpuppy)
Severity: -- → N/A
Flags: needinfo?(mixedpuppy)
Priority: -- → P5
Severity: N/A → S4
You need to log in before you can comment on or make changes to this bug.