From 2485aab12344557da4400d8139fcfb20bfd8c67b Mon Sep 17 00:00:00 2001 From: Jonathan Wyss Date: Sun, 17 Mar 2024 00:40:33 +0100 Subject: [PATCH] async functions working --- background.js | 34 ++++++++++++++++++++++++++-------- home.html | 2 +- manifest.json | 5 +++-- spawn.js | 15 ++++++++++----- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/background.js b/background.js index a48d703..bf32a72 100644 --- a/background.js +++ b/background.js @@ -1,17 +1,27 @@ let backgroundPage = browser.extension.getBackgroundPage(); - - +//let tab; +const pass_data = {urls:"urls",tab:"tab_id"} readUrl(); //on addon click load extension page // function Setup(data) { - browser.browserAction.onClicked.addListener(() => { - let promise = browser.tabs.create({"url": "home.html"}); - // promise.then((value) => run(value),()=> failure(value)); + let promise; + browser.browserAction.onClicked.addListener(() => { + + promise = browser.tabs.create({"url": "home.html"},function(tab){ + + browser.runtime.onMessage.addListener(()=>{ + run(data,tab); + console.log("pass tab data to run, in promise"); + console.log(tab); + }); + }); + + }); - browser.runtime.onMessage.addListener(()=>run(data)); + } function failure(value) @@ -19,9 +29,16 @@ function failure(value) console.log("tabs.create returned a failure"); } -function run(data) -{ +async function run(data,tab) +{ let i2=0; + // injectScript(tab); + await browser.scripting.executeScript({ + target:{ + tabId: tab.id + }, + files:["spawn.js"] + }); console.log("bbackground.js: received start signal..."); if(i2 == 0) { @@ -63,3 +80,4 @@ function readUrl() } + diff --git a/home.html b/home.html index 5ed967a..5144dd3 100644 --- a/home.html +++ b/home.html @@ -4,4 +4,4 @@ - \ No newline at end of file + diff --git a/manifest.json b/manifest.json index 70355ea..48293e8 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "version": "1.0", "description": "---", "web_accessible_resources":["newpipe_subscriptions_202403101004.json"], - "permissions":["tabs"], + "permissions":["tabs","scripting"], "icons": { "48": "icons/border-48.png" }, @@ -16,6 +16,7 @@ }, "background":{ "scripts": ["background.js"] - } + }, + "host_permissions":["*://youtube.com/*"] } diff --git a/spawn.js b/spawn.js index f12439d..263df83 100644 --- a/spawn.js +++ b/spawn.js @@ -2,17 +2,22 @@ //1.grant host permission // // -browser.runtime.onMessage.addListener((message,listener,handle) +browser.runtime.onMessage.addListener(function (message,listener) +{ + handle(message); +}); //if(number>0) -function handle() +function handle(message) { - if(message != 0)//on init only load page + console.log("inside handler"); + 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(); console.log(message); } - let url = message; - window.location.replace(url); + let url = message.url; console.log(url); + window.location.replace(url); + // window.location = url; }