background.js不再支持ajax,使用fetch替代
示例:
fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(data),
cache: 'no-cache',
credentials: 'same-origin',
mode: 'cors',
redirect: 'follow',
referrer: 'no-referrer',
}).then(function (response) {
console.log(response)
return response.json();
}).then(function (myJson) {
console.log(myJson);
});
--------------------分割线--------------------
background.js现支持模块化开发
这代表了我们可以使用更多的外部方法
或者减少background.js里的代码量
但只能加载本地的方法
首先需要开启权限
"background": {
"service_worker": "background.js",
"type": "module"
},
示例:
//background.js
import { abc } from './js/options.js'
console.log(abc)
//options.js
export let abc = "aaaaaaaa"