Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么模块导入会破坏Chrome扩展?_Javascript_Google Chrome_Google Chrome Extension_Module - Fatal编程技术网

Javascript 为什么模块导入会破坏Chrome扩展?

Javascript 为什么模块导入会破坏Chrome扩展?,javascript,google-chrome,google-chrome-extension,module,Javascript,Google Chrome,Google Chrome Extension,Module,我正在使用SunCalc.js代码进行重构。最初,我在background.js中保存了所有的SunCalc代码(扩展名也起作用了),但我想将SunCalc代码放在一个单独的文件SunCalc.js中 下面是新的background.js: import {sunModule as sunModule} from "./suncalc.js"; chrome.browserAction.onClicked.addListener(function(tab) { alert("Runni

我正在使用SunCalc.js代码进行重构。最初,我在
background.js
中保存了所有的SunCalc代码(扩展名也起作用了),但我想将SunCalc代码放在一个单独的文件
SunCalc.js

下面是新的
background.js

import {sunModule as sunModule} from "./suncalc.js";

chrome.browserAction.onClicked.addListener(function(tab) {
    alert("Running.");
    // Note that this may take a second:
    navigator.geolocation.getCurrentPosition(wasSuccessful, notSuccessful);
});

function wasSuccessful(position) {
    alert("Here");
    var theDate = new Date();
    var times = SunCalc.getTimes(new Date(), position.coords.latitude, position.coords.longitude);

    if ((theDate <= times.sunrise) || (times.sunset <= theDate)) {
        alert("It's night.");
    } else {
        alert("It's day.");
    };
}

function notSuccessful(err) {
    alert("Not Successful.");
}
从“/suncalc.js”导入{sunModule as sunModule};
chrome.browserAction.onClicked.addListener(函数(选项卡){
警惕(“奔跑”);
//请注意,这可能需要一秒钟:
navigator.geolocation.getCurrentPosition(wasSuccessful,notSuccessful);
});
功能成功(位置){
警报(“此处”);
var theDate=新日期();
var times=SunCalc.getTimes(新日期(),position.coords.latitude,position.coords.longitude);

如果((日期如wOxxOm所说,如果您想使用ES6模块化系统,您必须使用Webpack或汇总捆绑包。 否则,您可以使用globals重构这些模块,并将它们放入manifest.json中:

"background": {
    "scripts": ["background.js", "module1.js", "module2.js"]
}
另一种方法是创建background.html文件并指定所有需要的脚本(通常通过
标记)。 清单将类似于:

"background": {
    "page": "background.html"
}

后台页面还不支持模块,请看,@wOxxOm哦,这太奇怪了。建议我将整个模块推到
background.js
?或者使用webpack等。好的。谢谢@wOxxOmOkay太好了。谢谢@Deliaz
"background": {
    "page": "background.html"
}