3

Basically, there is a page that I visit that uses RequireJS. I want to make adjustments to this page, so I went the route of a userscript. While looking at the client-side code I see that there is a module defined as so:

define("settings", [], function() {
    return {
    SETTINGA: "100",
    SETTINGB: "200",
    etc.
    }
})

I want to add my own item to the settings array, as well as change some settings without having to redefine the module in my userscript (with the changes) and then removing/readding it. Is it possible to just make adjustments to this module?


P.S. I'm using the Script Injection technique to get my userscript to interact with other javascript in the original page.

Also, doing require("settings") in the Javascript console returns an object (not an array), so I can't do things like require("settings")[0] or require("settings").push(...), however I can access the settings by doing require("settings").SETTINGA. So, I'm not sure how to add/redefine settings to this since it is not an array?

Community
  • 1
  • 1
myermian
  • 31,823
  • 24
  • 123
  • 215

2 Answers2

4

Use this:

require('settings').new_property = 'new value';
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
ziad-saab
  • 19,139
  • 3
  • 36
  • 31
  • Thanks! After weeding through much more complex solutions on the web, it was great to see such a simple solution. I'm now using this method to stub-out internal dependencies of my components under test. – Dave Cadwallader Jun 14 '12 at 19:10
2

The reason you can't retrieve or add settings to "the settings array" is that you aren't creating an array to begin with. {foo:bar} is an object literal, not an array literal ([foo,bar]).

Petter Häggholm
  • 520
  • 1
  • 3
  • 12