async functions working

This commit is contained in:
Jonathan Wyss 2024-03-17 00:40:33 +01:00
parent e8b0022302
commit 2485aab123
4 changed files with 40 additions and 16 deletions

View File

@ -1,17 +1,27 @@
let backgroundPage = browser.extension.getBackgroundPage(); let backgroundPage = browser.extension.getBackgroundPage();
//let tab;
const pass_data = {urls:"urls",tab:"tab_id"}
readUrl(); readUrl();
//on addon click load extension page //on addon click load extension page
// //
function Setup(data) function Setup(data)
{ {
browser.browserAction.onClicked.addListener(() => { let promise;
let promise = browser.tabs.create({"url": "home.html"}); browser.browserAction.onClicked.addListener(() => {
// promise.then((value) => run(value),()=> failure(value));
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) function failure(value)
@ -19,9 +29,16 @@ function failure(value)
console.log("tabs.create returned a failure"); console.log("tabs.create returned a failure");
} }
function run(data) async function run(data,tab)
{ {
let i2=0; let i2=0;
// injectScript(tab);
await browser.scripting.executeScript({
target:{
tabId: tab.id
},
files:["spawn.js"]
});
console.log("bbackground.js: received start signal..."); console.log("bbackground.js: received start signal...");
if(i2 == 0) if(i2 == 0)
{ {
@ -63,3 +80,4 @@ function readUrl()
} }

View File

@ -4,4 +4,4 @@
<button type="submit" id="id_run" value="run importer" name="b_submit">RUNRUNRUN</button> <button type="submit" id="id_run" value="run importer" name="b_submit">RUNRUNRUN</button>
<script type="text/javascript" language="javascript" src="home.js"></script> <script type="text/javascript" language="javascript" src="home.js"></script>
</body> </body>
</html> </html>

View File

@ -4,7 +4,7 @@
"version": "1.0", "version": "1.0",
"description": "---", "description": "---",
"web_accessible_resources":["newpipe_subscriptions_202403101004.json"], "web_accessible_resources":["newpipe_subscriptions_202403101004.json"],
"permissions":["tabs"], "permissions":["tabs","scripting"],
"icons": { "icons": {
"48": "icons/border-48.png" "48": "icons/border-48.png"
}, },
@ -16,6 +16,7 @@
}, },
"background":{ "background":{
"scripts": ["background.js"] "scripts": ["background.js"]
} },
"host_permissions":["*://youtube.com/*"]
} }

View File

@ -2,17 +2,22 @@
//1.grant host permission //1.grant host permission
// //
// //
browser.runtime.onMessage.addListener((message,listener,handle) browser.runtime.onMessage.addListener(function (message,listener)
{
handle(message);
});
//if(number>0) //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(); 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; let url = message.url;
window.location.replace(url);
console.log(url); console.log(url);
window.location.replace(url);
// window.location = url;
} }