Code Monkey home page Code Monkey logo

Comments (9)

tophf avatar tophf commented on July 18, 2024

Works for me in Chrome, so I guess Firefox doesn't implement it fully.

from violentmonkey.

aminomancer avatar aminomancer commented on July 18, 2024

It works fine in Firefox, just not in a userscript. I've tested it in console and I could also test it in a webextension. Is there some reason why it would work elsewhere but not in a userscript?

from violentmonkey.

tophf avatar tophf commented on July 18, 2024

I was wrong, it's the same in Chrome. I was confused by the Play button being ignored in Firefox (I had to restart it).

So apparently youtube overrides your handler and you need to run your code later?

from violentmonkey.

tophf avatar tophf commented on July 18, 2024

In other words, this is how the API is designed and I don't see anything we can do about it.

from violentmonkey.

aminomancer avatar aminomancer commented on July 18, 2024

Honestly I don't think it's that. That was one of the things I thought might be the issue in my troubleshooting, so I also tried this pattern:

let addListener = () => navigator.mediaSession.setActionHandler("play", function () {
  console.log("GREETINGS;");
});

addListener();

setTimeout(addListener, 10000);

As well as testing document-idle and document-end for @run-at.

I'm not sure youtube is setting an action handler at all. Firefox natively handles media keys on all sites. It's part of the basic implementation and toggled by the pref media.hardwaremediakeys.enabled. So the fact that media keys work on youtube doesn't mean youtube is handling them; Firefox just handles media keys for all media elements.

from violentmonkey.

tophf avatar tophf commented on July 18, 2024

But your new code works for me and I don't see any other theoretically possible explanation.

from violentmonkey.

aminomancer avatar aminomancer commented on July 18, 2024

In that case I suppose you're right. It doesn't work for me, but maybe I need to try other asynchronous methods. It feels like youtube's loading is somehow hampered by the timeout, even though it should not be blocking. Maybe the script is causing youtube to set its action handler later than usual.

I'll see what else I can do. Thanks for testing.

I suppose I could set my handlers and then no-op the setActionHandler method.

from violentmonkey.

tophf avatar tophf commented on July 18, 2024

Judging by the documentation for setActionHandler, this is how it's designed to work. It's not like addEventListener, but like elem.onclick = foo which overwrites the old listener.

You'll have to implement cascading manually, e.g.:

const playCallbacks = new Set();
const M = navigator.mediaSession;
M.setActionHandler("play", info => {
  console.log("GREETINGS;");
  for (const fn of playCallbacks) fn(info); // IDK about try-catch handling
});
M.setActionHandler = new Proxy(M.setActionHandler, {
  apply(fn, thisArg, args) {
    if (args[0] === 'play') playCallbacks.add(args[1]);
    else Reflect.apply(fn, thisArg, args);
  }
});

from violentmonkey.

aminomancer avatar aminomancer commented on July 18, 2024

Thanks! Yeah that makes sense. I won't need cascading since I just want to override nexttrack and previoustrack (youtube's shuffling gets stuck in loops sometimes), but I can do that too with a proxy. By breaking on setActionHandler I found that youtube actually sets the action handlers 3+ times, starting early and continuously attempting it at least 2 times. Kinda weird but I guess that explains it.

Seems to work using this approach: https://github.com/aminomancer/userscripts/blob/main/betterYouTubeMediaKeyHandlers.user.js

from violentmonkey.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.