Closed Bug 916780 Opened 7 years ago Closed 7 years ago

Uncaught ReferenceError: Localized is not defined


(Webmaker :: X-Ray Goggles, defect)

Mac OS X
Not set


(Not tracked)



(Reporter: alicoding, Unassigned)




(1 file)

Some of the site prevent the script to load and the console error is: Uncaught ReferenceError: Localized is not defined 

I believe that the site prevent the script to be attached globally and that makes our script not working properly.
The site that I tried and it doesn't work.
The problem here is that these sites use requirejs, and so this section of our Localized.js does the wrong thing:

 if (typeof define === 'function' && define.amd) {
  // Expose a global instead
  else {
    global.Localized = factory();

In most cases (no requirejs on the page), we'll attach to the global window object.  However, if requirejs is there, we'll define a module, which will get lost when the rest of the webxray code goes looking for it on window.

Somehow we have to force putting this on the global all the time in the webxray case.
I can see various hacky ways to solve this, and I'm not sure which way we want to go.

Essentially, we need a trick to force Localized to get added to window in certain cases, and we need to do it without giving localized.js special knowledge of Goggles as such.

Here's one suggestion:

1) Given, modify it so it looks for some sort of global flag indicating it should force things on the global, for example:

// Global var defined somewhere outside localized.js

// localized.js modified to look for this global
(function(global, factory) {
  // AMD. Register as an anonymous module. Also deal with the case
  // that we've been told to force localized on the global (e.g.,
  // in cases where require.js might exist in a page and we want to
  // ignore it and use the global instead).
  if (typeof define === 'function'    &&
      define.amd                      &&
  // Expose a global instead
  else {
    global.Localized = factory();
}(this, function() {

2) Create a new file in Goggles that gets added above, and looks like this:

(function(global) {
  // Configure localized.js to put Localized on the global and
  // ignore the presence of require.js in pages it gets loaded into.

3) Put that new file into the build system (config.js) above localized.js

Pomax, can you see any other way to do this?
Flags: needinfo?(pomax)
Could we put a property on the factory function itself? Something that allows use to call factory.asGlobal(), which ensures that even if it is require.js loaded, it then also tacks itself onto the global context if it's not already there, returning itself as function apoint. This would allow us to call it as "require("localise")" for most cases, but would allow us to use "require("localise").asGlobal()" in things like webxray.js
Flags: needinfo?(pomax)
I have no idea how "output" became "apoint", but s/apoint/output/ ...
Fixed from this bug:
Closed: 7 years ago
QA Contact: ali
Resolution: --- → FIXED
This won't fix it yet.  We have to chagne Goggles to force `window.__LOCALIZED_IGNORE_REQUIREJS = true;`
Resolution: FIXED → ---
Duplicate of this bug: 919985
Attachment #809226 - Flags: review?(pomax)
Attachment #809226 - Flags: review+
Duplicate of this bug: 920147
Attachment mime type: text/plain → text/x-github-pull-request
Landed and fixed.
Closed: 7 years ago7 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.