iterration works

This commit is contained in:
Jonathan Wyss 2024-03-17 18:11:28 +01:00
parent 3a148716b2
commit acd94cb7ab
2 changed files with 56 additions and 27 deletions

View File

@ -7,13 +7,12 @@ readUrl();
// //
function Setup(data) function Setup(data)
{ {
let promise; let i=0;
browser.browserAction.onClicked.addListener(() => { browser.browserAction.onClicked.addListener(() => {
promise = browser.tabs.create({"url": "home.html"},function(tab){ browser.tabs.create({"url": "home.html"},function(tab){
browser.runtime.onMessage.addListener(()=>{ browser.runtime.onMessage.addListener(()=>{
run(data,tab); nextUrl(data,tab,i);
console.log("pass tab data to run, in promise"); console.log("pass tab data to run, in promise");
console.log(tab); console.log(tab);
}); });
@ -32,24 +31,49 @@ function failure(value)
function run(data,tab) function run(data,tab)
{ {
let i2=0; let i2=0;
sendMessageToContent(data,tab,i2);
// injectScript(tab); // injectScript(tab);
let promise = browser.tabs.executeScript(tab.id,{ //nextUrl()
file:"spawn.js" }
}); async function nextUrl(data,tab,i)
promise.then(function() { {
console.log("bbackground.js: received start signal..."); let count_urls = data.subscriptions.length;
if(i2 == 0) for(i=0;i<count_urls;){
{ await sendMessageToContent(data,tab,i);
browser.tabs.sendMessage(tab.id,{state: "Init",url: data.subscriptions[0].url}); console.log("iterating");
} console.log(i);
else i++;
{ }
browser.tabs.sendMessage(tab.id,{state: i2,url: data.subscriptions[i2].url}); }
} function sendMessageToContent(data,tab,i)
i2++; {
return new Promise(resolve => {
browser.tabs.executeScript(tab.id,{
file:"spawn.js"
})
.then(function() {
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();
});
}
});
}); });
} }
function readUrl() function readUrl()
{ {
let data; let data;

View File

@ -2,17 +2,22 @@
//1.grant host permission //1.grant host permission
// //
// //
browser.runtime.onMessage.addListener((message) => browser.runtime.onMessage.addListener(worker);
// return Promise.resolve(true);
/*.then(function(){
window.location.replace(url);
});*/
function worker(message,sender,sendResponse)
{ {
console.log("inside handler"); console.log("inside handler");
if(message.state != "Init")//on init only load page if(message.state != "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; let url = message.url;
console.log(url); console.log(url);
window.location.replace(url); sendResponse(true);
window.location.replace(url);
}); }