2024-01-03 11:36:26 +00:00
|
|
|
/*
|
|
|
|
* extension process crashes or your extension is manually stopped at
|
|
|
|
* chrome://serviceworker-internals
|
|
|
|
*/
|
2024-01-01 15:58:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-01-03 11:36:26 +00:00
|
|
|
const storage = chrome.storage.local;
|
|
|
|
|
2023-12-31 18:40:47 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(function(){
|
2024-01-05 02:29:39 +00:00
|
|
|
console.log("onInstalled");
|
|
|
|
|
|
|
|
let default_status='ON';
|
|
|
|
chrome.action.setBadgeText({
|
|
|
|
text: default_status
|
|
|
|
});
|
|
|
|
|
2023-12-31 18:40:47 +00:00
|
|
|
fetch("data/settings.json")
|
|
|
|
.then((resp) => resp.json())
|
|
|
|
.then((settings) =>
|
|
|
|
{
|
|
|
|
chrome.storage.local.set(
|
|
|
|
{
|
2024-01-05 02:29:39 +00:00
|
|
|
settings: settings,
|
|
|
|
status: default_status
|
2023-12-31 18:40:47 +00:00
|
|
|
}
|
|
|
|
);
|
2024-01-05 02:29:39 +00:00
|
|
|
console.log("dump settings.json to storage");
|
2023-12-31 18:40:47 +00:00
|
|
|
}
|
|
|
|
);
|
2024-01-05 02:29:39 +00:00
|
|
|
});
|
2024-01-03 11:36:26 +00:00
|
|
|
|
2024-01-05 02:29:39 +00:00
|
|
|
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((e) => {
|
|
|
|
const msg = `Navigation blocked to ${e.request.url} on tab ${e.request.tabId}.`;
|
|
|
|
//console.log(msg);
|
2024-01-03 11:36:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
chrome.action.onClicked.addListener(async (tab) => {
|
|
|
|
const prevState = await chrome.action.getBadgeText({ tabId: tab.id });
|
|
|
|
// Next state will always be the opposite
|
|
|
|
const nextState = prevState === 'ON' ? 'OFF' : 'ON';
|
|
|
|
storage.set({status: nextState});
|
|
|
|
|
|
|
|
// Set the action badge to the next state
|
|
|
|
await chrome.action.setBadgeText({
|
|
|
|
tabId: tab.id,
|
|
|
|
text: nextState
|
|
|
|
});
|
2024-01-01 15:58:30 +00:00
|
|
|
});
|
2024-01-05 02:29:39 +00:00
|
|
|
|
|
|
|
import heartbeatconnect from './modules/heartbeatconnect.js';
|
|
|
|
|
|
|
|
let heartbeatInterval;
|
|
|
|
|
|
|
|
async function runHeartbeat()
|
|
|
|
{
|
|
|
|
//console.log("runHeartbeat");
|
|
|
|
storage.get('status', function (items)
|
|
|
|
{
|
|
|
|
console.log(items);
|
|
|
|
if (items.status && items.status=='ON')
|
|
|
|
{
|
|
|
|
heartbeatconnect.start();
|
|
|
|
} else {
|
|
|
|
console.log('no status found');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function startHeartbeat()
|
|
|
|
{
|
|
|
|
runHeartbeat().then(() =>
|
|
|
|
{
|
|
|
|
heartbeatInterval = setInterval(runHeartbeat, 1 * 1000);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stopHeartbeat()
|
|
|
|
{
|
|
|
|
clearInterval(heartbeatInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
startHeartbeat();
|