inporter worksvim background.js !

This commit is contained in:
Jonathan Wyss 2024-03-18 21:22:16 +01:00
parent 96be5a4418
commit 901ead6691
3 changed files with 82 additions and 39 deletions

View File

@ -1,42 +1,70 @@
let backgroundPage = browser.extension.getBackgroundPage(); let backgroundPage = browser.extension.getBackgroundPage();
//let tab; //let tab;
const pass_data = {urls:"urls",tab:"tab_id"} const pass_data = {data:"urls",tab:"tab_id"}
let data;
readUrl();
//on addon click load extension page //on addon click load extension page
// //
function Setup(data) let i=0;
{
let i=0;
browser.browserAction.onClicked.addListener(function(event){
create(data);
});
}
var create = (function(data){
let i=0; data = readUrl()
.then(function(data){
browser.browserAction.onClicked.addListener(function(event1){
browser.tabs.create({"url": "home.html"},function(tab){ browser.tabs.create({"url": "home.html"},function(tab){
browser.runtime.onMessage.addListener((message)=>{
if(message === "Button_clicked")
{
console.log("pass tab data to run, in promise");
console.log(tab);
} browser.runtime.onMessage.addListener(function(request,sender,sendResponse){
else if(message === "Content_Loaded") handleMessages(request,sender,sendResponse,tab,data);
{
console.log("Received Content_Loaded");
nextUrl(data,tab,i);
}
}); });
}); });
});
}); });
function failure(value) function handleMessages(message,sender,sendResponse,tab,data){
{
console.log("tabs.create returned a failure"); 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);
}
} }
/////////////////////
async function nextUrl(data,tab,i) async function nextUrl(data,tab,i)
{ {
let count_urls = data.subscriptions.length; let count_urls = data.subscriptions.length;
@ -44,7 +72,8 @@ async function nextUrl(data,tab,i)
await sendMessageToContent(data,tab,i); await sendMessageToContent(data,tab,i);
code1 = `window.location.replace("${data.subscriptions[i].url}");` code1 = `window.location.replace("${data.subscriptions[i].url}");`
console.log(code1); console.log(code1);
await browser.tabs.update(tab.tabid,{url : data.subscriptions[i].url}); console.log(tab);
await browser.tabs.update(tab.id,{url : data.subscriptions[i].url});
await waitforTab(tab,data.subscriptions[i].url); await waitforTab(tab,data.subscriptions[i].url);
console.log(tab); console.log(tab);
console.log("iterating"); console.log("iterating");
@ -75,7 +104,6 @@ function sendMessageToContent(data,tab,i)
}) })
.then(function() { .then(function() {
console.log("background.js: received start signal..."); console.log("background.js: received start signal...");
console.log("backgroundscript received start signal from contentscript");
if(i == 0) if(i == 0)
{ {
browser.tabs.sendMessage(tab.id,{state: "Init",url: data.subscriptions[0].url}) browser.tabs.sendMessage(tab.id,{state: "Init",url: data.subscriptions[0].url})
@ -99,6 +127,7 @@ function sendMessageToContent(data,tab,i)
function readUrl() function readUrl()
{ {
let data; let data;
return new Promise(resolve => {
const filePath = "newpipe_subscriptions_202403101004.json"; const filePath = "newpipe_subscriptions_202403101004.json";
var localURL = browser.extension.getURL(filePath); var localURL = browser.extension.getURL(filePath);
fetch(localURL).then((res) => { fetch(localURL).then((res) => {
@ -119,10 +148,10 @@ function readUrl()
catch(e) {InsertError(e.description);} catch(e) {InsertError(e.description);}
} }
console.log(data); console.log(data);
Setup(data); resolve(data);
}) })
.catch((error)=> console.error("couldnt fetch data",error)); .catch((error)=> console.error("couldnt fetch data",error));
});
} }

View File

@ -23,9 +23,8 @@
"matches": [ "matches": [
"<all_urls>" "<all_urls>"
], ],
"js": [ "js": ["spawn.js"],
"spawn.js" "run_at":"document_end"
]
} }
] ]
} }

View File

@ -4,25 +4,40 @@
// //
document.addEventListener("DOMContentLoaded",function(){ //document.addEventListener("DOMContentLoaded",function(){
browser.runtime.sendMessage("Content_Loaded"); browser.runtime.sendMessage("Content_Loaded");
});
browser.runtime.onMessage.addListener(worker); browser.runtime.onMessage.addListener(worker);
//});
// return Promise.resolve(true); // return Promise.resolve(true);
/*.then(function(){ /*.then(function(){
window.location.replace(url); window.location.replace(url);
});*/ });*/
function worker(message,sender,sendResponse) function test(msg)
{
console.log(`Spawn: received message:${msg}`);
}
function worker(message)
{ {
console.log("inside handler"); console.log("inside handler");
if(message.state != "Init")//on init only load page console.log(message);
setTimeout(() => {
if(message != "Init")//on init only load page
{ {
document.getElementsByClassName("yt-spec-button-shape-next yt-spec-button-shape-next--filled yt-spec-button-shape-next--mono yt-spec-button-shape-next--size-m")[0].click(); document.getElementsByClassName("yt-spec-button-shape-next yt-spec-button-shape-next--filled yt-spec-button-shape-next--mono yt-spec-button-shape-next--size-m")[0].click();
console.log(message); console.log(message);
} }
let url = message.url;
browser.runtime.sendMessage("finished");
},5000);
let url = message;
console.log(url); console.log(url);
sendResponse(true);
} }