WebExtension/background.js

158 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-03-14 21:11:08 +00:00
let backgroundPage = browser.extension.getBackgroundPage();
2024-03-16 23:40:33 +00:00
//let tab;
2024-03-18 20:22:16 +00:00
const pass_data = {data:"urls",tab:"tab_id"}
let data;
2024-03-15 10:03:09 +00:00
//on addon click load extension page
//
2024-03-18 20:22:16 +00:00
let i=0;
2024-03-16 23:40:33 +00:00
2024-03-18 20:22:16 +00:00
data = readUrl()
.then(function(data){
browser.browserAction.onClicked.addListener(function(event1){
2024-03-18 10:17:23 +00:00
browser.tabs.create({"url": "home.html"},function(tab){
2024-03-18 10:39:52 +00:00
2024-03-15 10:03:09 +00:00
2024-03-18 20:22:16 +00:00
browser.runtime.onMessage.addListener(function(request,sender,sendResponse){
handleMessages(request,sender,sendResponse,tab,data);
2024-03-18 10:17:23 +00:00
});
2024-03-18 20:22:16 +00:00
2024-03-18 10:17:23 +00:00
});
2024-03-18 20:22:16 +00:00
});
2024-03-18 10:17:23 +00:00
});
2024-03-18 20:22:16 +00:00
function handleMessages(message,sender,sendResponse,tab,data){
if(message === "Button_clicked")
{
browser.tabs.executeScript(tab.id,{
file:"spawn.js"
});
console.log("pass tab data to run, in promise");
console.log(tab);
console.log(data);
//nextUrl(data,tab,i);
}
else if(message === "Content_Loaded")
{
console.log("Background: Received Content_Loaded");
// nextUrl(data,tab,i);
if(i===0)
{
browser.tabs.sendMessage(tab.id,"Init");
}
else
{
browser.tabs.sendMessage(tab.id,data.subscriptions[i].url);
}
}
else if(message === "finished")
{
i++;
console.log("background: received next from spawn.js");
browser.tabs.update(tab.id,{url : data.subscriptions[i].url});
browser.tabs.executeScript(tab.id,{
file:"spawn.js"
});
browser.tabs.sendMessage(tab.id,data.subscriptions[i].url);
}
2024-03-14 21:11:08 +00:00
}
2024-03-18 20:22:16 +00:00
/////////////////////
2024-03-17 17:11:28 +00:00
async function nextUrl(data,tab,i)
{
let count_urls = data.subscriptions.length;
for(i=0;i<count_urls;){
await sendMessageToContent(data,tab,i);
2024-03-17 17:57:18 +00:00
code1 = `window.location.replace("${data.subscriptions[i].url}");`
console.log(code1);
2024-03-18 20:22:16 +00:00
console.log(tab);
await browser.tabs.update(tab.id,{url : data.subscriptions[i].url});
2024-03-18 10:39:52 +00:00
await waitforTab(tab,data.subscriptions[i].url);
2024-03-18 10:17:23 +00:00
console.log(tab);
2024-03-17 17:11:28 +00:00
console.log("iterating");
console.log(i);
i++;
2024-03-18 10:17:23 +00:00
2024-03-17 17:11:28 +00:00
}
2024-03-15 10:03:09 +00:00
}
2024-03-18 10:17:23 +00:00
function waitforTab(tab,url)
{
return new Promise(resolve =>{
setTimeout(() => {
console.log(tab.url);
if(tab.url === url){
console.log("Resolved wait for tab)");
resolve();
}
},5000);
});
}
2024-03-17 17:11:28 +00:00
function sendMessageToContent(data,tab,i)
{
return new Promise(resolve => {
2024-03-14 21:11:08 +00:00
2024-03-17 17:11:28 +00:00
browser.tabs.executeScript(tab.id,{
file:"spawn.js"
})
.then(function() {
2024-03-18 10:17:23 +00:00
console.log("background.js: received start signal...");
if(i == 0)
{
browser.tabs.sendMessage(tab.id,{state: "Init",url: data.subscriptions[0].url})
.then((response) => {
console.log("got response!! content should reload");
console.log(response);
resolve();
});
}
else
{
browser.tabs.sendMessage(tab.id,{state: i,url: data.subscriptions[i].url})
.then((response) => {
console.log(response);
resolve();
});
}
2024-03-17 17:11:28 +00:00
});
});
}
2024-03-15 10:03:09 +00:00
function readUrl()
{
let data;
2024-03-18 20:22:16 +00:00
return new Promise(resolve => {
2024-03-15 10:03:09 +00:00
const filePath = "newpipe_subscriptions_202403101004.json";
var localURL = browser.extension.getURL(filePath);
fetch(localURL).then((res) => {
if(!res.ok){
throw new Error
(`res error`);
}
return res.json();
})
.then((data) => {
for(let i=0;i<i;i++) //i in data.subscriptions)
{
let open=null;
console.log(data.subscriptions[i].url);
try{
open = alert(data.subscriptions[i].url,"_blank");
}
catch(e) {InsertError(e.description);}
}
console.log(data);
2024-03-18 20:22:16 +00:00
resolve(data);
2024-03-15 10:03:09 +00:00
})
.catch((error)=> console.error("couldnt fetch data",error));
2024-03-14 21:11:08 +00:00
2024-03-18 20:22:16 +00:00
});
2024-03-15 10:03:09 +00:00
}
2024-03-16 23:40:33 +00:00