gpt4 book ai didi

javascript - 如何从同一文件夹将 js 文件导入到 chrome 扩展中的 background.js

转载 作者:行者123 更新时间:2023-12-05 00:26:33 26 4
gpt4 key购买 nike

我在导入与“background.js”(脚本库)相同的库中的文件“score.js”时遇到问题。
我是 js 和 chrome 扩展的新手。
我查看了 require.js 并做到了这一点。
背景.html:

<!doctype html>
<html>
<head>
<title>Tab Manager</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Tab Manager</h1>
<script data-main="scripts/score.js" src="scripts/require.js"></script>
</body>
</html>
背景.js:
// gets a dictionary of existing windows ids as keys and tab arrays as values.
function get_windows()
{
var window_dict = {};
chrome.windows.getAll({populate:true}, windowArr =>{
var i;
for (i = 0; i < windowArr.length; i++) {
var window = windowArr[i];
var window_id = window.id;
var tabs = window.tabs;
window_dict[window_id] = tabs;
}
console.log(window_dict);
main(window_dict);
})

}

function starting_scores(window_dict)
{
var scores = [];
var i;
var j = 0;
var total_tabs = [];
for (const [window_id, tabs] of Object.entries(window_dict)) {
for (i = 0; i < tabs.length; i++){
scores[j] = new Score(tabs[i], window_id);
j = j++;
}}
return scores;
}

function main(window_dict)
{
var scores = starting_scores(window_dict);
console.log(scores);
}

get_windows();
score.js:
class Score
{
// Constructor
constructor(tab, window_id)
{
this.window_id = window_id; // The window id of the window that contains the tab.
this.tab = tab;
this.url = tab.url;
this.active = tab.active; // If the tab is currently looked at then it's active.
this.audible = tab.audible; // If the tab is playing audio then audible will be true.
this.discarded = tab.discarded; // If the tab is already unloaded but not closed this will be true.

/* If the new tab is active then the timer is set to zero because
the user hasn't spent any time not using the tab. Otherwise a timer starts. */
if(this.active == true){
this.timer == 0;}
else{
this.timer = performance.now;}
}

// Get methods
getWindowId() {
return this.window_id;
}
getTab() {
return this.tab;
}
getUrl() {
return this.url;
}
getActive() {
return this.active;
}
getAudible() {
return this.audible;
}
getDiscarded() {
return this.discarded;
}
getTimer() {
return this.timer;
}

// Set methods
setWindowId(window_id) {
this.window_id = window_id;
}
setTab(tab) {
this.tab = tab;
}
setUrl(url) {
this.url = url;
}
setActive(active) {
this.active = active;
}
setAudible(audible) {
this.audible = audible;
}
setDiscarded(discarded) {
this.discarded = discarded;
}
setTimer(timer) {
this.timer = timer;
}
}
我正在尝试构建一个扩展,为每个选项卡的使用打分,然后删除得分低的那些。不要介意我无法让 score.js 在 background.js 中工作的逻辑
任何帮助都将不胜感激。

最佳答案

@wOxxOm's answer 的更新,从 Manifest v3 开始,您不能使用 background.page ,但您可以直接将脚本作为模块加载。
manifest.json

"background": {
"service_worker": "background.js",
"type": "module"
}
背景.js
import Score from './score.js';
score.js
export default class Score

关于javascript - 如何从同一文件夹将 js 文件导入到 chrome 扩展中的 background.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67510470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com