Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefining a RequireJS module -- is it possible?

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?

like image 524
myermian Avatar asked Sep 05 '26 04:09

myermian


2 Answers

Use this:

require('settings').new_property = 'new value';
like image 139
ziad-saab Avatar answered Sep 07 '26 17:09

ziad-saab


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]).

like image 20
Petter Häggholm Avatar answered Sep 07 '26 18:09

Petter Häggholm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!