Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

javascript - Unable to use Components in WebExtensions: get "ReferenceError: Cu is not defined"

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can NOT use Components in WebExtensions

For the older Firefox extension types, the Components Object provided access to low-level capabilities in Firefox. The common aliases are:

  • Cc = Components.classes
  • Ci = Components.interfaces
  • Cu = Components.util
  • Cr = Components.results
  • Cm = Components.manager
  • components = Components

In 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 WebExtensions

Access 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:

MDN old extension warning

If you need something that exists in older extensions, but not in WebExtensions, try a WebExtensions Experiment

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...