重构Crossrider扩展代码

Pot*_*een 2 javascript cross-browser include browser-extension crossrider

我使用Crossrider构建了我的扩展,目前我的所有代码都在extension.js文件中.但是,它在这个单片文件中变得越来越难以维护.有没有办法将我的代码拆分成单独的文件,仍然在我的扩展中使用它们?

因此,例如,如果我的extension.js文件的结构类似于以下示例,我希望函数f1和f2位于我可以加载到扩展中的单独文件中:

appAPI.ready(function($) {
  init();
  f1();
  f2();
  ...

  function init() {
    // init code
    ...
  }
  function f1() {
    //hundreds of lines of code
  }
  function f2() {
    //hundreds of lines of code
  }
  ...
});
Run Code Online (Sandbox Code Playgroud)

Shl*_*omo 5

您可以在一个或多个资源文件(例如init.js,functions.js)中定义函数,然后在appAPI.ready中包含它.有关资源的更多信息,请参阅appAPI.resources.

因此,使用您的示例,您的代码将类似于:

extension.js:

appAPI.ready(function($) {
  appAPI.resources.includeJS('init.js');
  appAPI.resources.includeJS('functions.js');
  init();
  f1();
  f2();
  ...
});
Run Code Online (Sandbox Code Playgroud)

init.js:

function init() {
  // init code
  ...
}
Run Code Online (Sandbox Code Playgroud)

functions.js:

function f1() {
  //hundreds of lines of code
}
function f2() {
  //hundreds of lines of code
}
Run Code Online (Sandbox Code Playgroud)

[ 披露:我是Crossrider员工]