How can I add a pause to a youtube video when it's opened using Hey Vicki (Alexa/Chrome extension) - Node.JS?

Viewed 23

I'm really new to coding, but trying to learn as much as I can.

I'm working on a personal script using heyviki (Alexa/chrome extension). I have the code working to open a new tab with the video that I want it to show.

Trying to get the video to pause as soon as it's opened.

I've tried adding the const delay... It's not kicking out any errors, but the script isn't pausing my video. And Alexa is saying "uh oh there's an error..."

Any help would be greatly appreciated. (Full script below)

// ==VickiScript==
// @name Open 20 minute yoga
// @id heyvicki.tab-opener
// @utterance Ask $invocation to open 20 minute yoga
// @icon /res/icons/script_web.png
// @permission <all_urls>
// @permission native
// @description Opens a 20 minute yoga in a new tab and pause.
// ==/VickiScript==

$vs.listen(/^(?:open 20 minute yoga)s?$/, async (req, res) => {
 

  const tab = await browser.tabs.create({ url: "https://www.youtube.com/watch?v=SzxdA2xFfL0", active: true });
  
  await pause (res, "pause", async () => {
  res.say("zen").send();
  
  const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
 await delay(4000); /// waiting 4 second.
 
 
  await pause();

  });

  res.send();
  
  

  
});


async function pause () {
  
  if (!$vs.native.available) {
    return;
    
  }

  await $vs.native.keyboard.click("{MEDIAPLAYPAUSE}");


  res.send();

};

EDIT// Played and played and played and this is what I got to finally make it work.

Changing to this code of 1 second delay seemed to do the trick :)

await new Promise(resolve => setTimeout(resolve, 1000));

Full code now::

// ==VickiScript==
// @name Open 20 minute yoga
// @id heyvicki.tab-opener
// @utterance Ask $invocation to open 20 minute yoga
// @icon /res/icons/script_web.png
// @permission <all_urls>
// @permission native
// @description Opens a 20 minute yoga in a new tab and pause.
// ==/VickiScript==

$vs.listen(/^(?:open 20 minute yoga|open a 20 minute yoga)s?$/, async (req, res) => {

  const tab = await browser.tabs.create({ url: "https://www.youtube.com/watch?v=SzxdA2xFfL0", active: true });


    //this, along with function sleep(ms) are supposed to 'sleep' the code for (3) seconds
await new Promise(resolve => setTimeout(resolve, 1000));

  await pause (res, "pause", async () => {
  res.say("zen").send();

  });

  res.send();
  
});


async function pause (res) {
  
  if (!$vs.native.available) {
    return;
    
  }

await $vs.native.keyboard.click("{MEDIAPLAYPAUSE}");


  res.send();

};
0 Answers
Related