I have a simple extension where I use Cu.import to import a JavaScript code module. But, when I load the extension, I get this error:
Cu is not defined
The code I was trying to use was:
Cu.import("resource://gre/modules/MatchPattern.jsm");
Cu.import("resource://gre/modules/BrowserUtils.jsm");
var regExArray = [];
var myArray = ["facebook.com", "google.com"];
var myURL="http://www.google.co.uk/?gfe_rd";
for (var x=0; x<myArray.length; x++)
{
console.log("loop: "+x);
var match = new MatchPattern(/(http:\/\/)(.*\.)*(myArray[x])(\/.*)*(\/)*/);
log("match result is: "+match.matches(myURL));
}//end for loop
I know how to define Cu in the Firefox Add-on SDK using require, but how can define it in WebExtesnions?
Components in WebExtensionsFor the older Firefox extension types, the Components Object provided access to low-level capabilities in Firefox. The common aliases are:
Cc = Components.classesCi = Components.interfacesCu = Components.utilCr = Components.resultsCm = Components.managercomponents = ComponentsIn the Add-on SDK these were described in the Chrome Authority documentation and were available by using
var {Cc, Ci, Cu, Cr, Cm, components} = require("chrome");
Components does not exist in WebExtensionsAccess to these low-level capabilities does not exist in WebExtensions. Remove it from your code and forget about it. Don't try to use anything connected to it, because you can't do so. Removing access to the low-level capabilities provided by Components is one of the specific reasons for moving to WebExtensions.
If you see it in a page on MDN, then that page is not about WebExtensions and should be ignored for WebExtensions development. You should see at the top of every such page a large warning stating that you should be using WebExtensions instead of the technology described on that page. The warning currently (2017-06-17) looks like:

You can extend the capabilities of WebExtensions by constructing a WebExtensions Experiment. The intent is to allow add-on developers to create new APIs for WebExtensions which are proposed for inclusion into Firefox. However, there is no guarantee that such proposals will actually be integrated into Firefox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With