Comments (9)
Works for me in Chrome, so I guess Firefox doesn't implement it fully.
from violentmonkey.
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.
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.
In other words, this is how the API is designed and I don't see anything we can do about it.
from violentmonkey.
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.
But your new code works for me and I don't see any other theoretically possible explanation.
from violentmonkey.
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.
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.
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)
- [BUG] Can't create a new userscript HOT 1
- [BUG] GM_download does not ignore CORS in iframe [NSFW]
- [BUG] Impossible to access to ViolentMonkey settings from ViolentMonkey current webpage script list HOT 3
- Release 2.19.0 for Edge HOT 1
- [Feature] User profiles HOT 3
- [Feature] Setting to allow XHR responses to set cookies HOT 16
- [BUG] Violentmonkey refuse to execute my script on google on opera browser HOT 15
- [BUG] v2.19.1 BETA reported an error and could not run the script.
- [Feature] Sync with local directory HOT 6
- [BUG] 2.19.2 works fine in FF, refuses to install on Edge Chromium Canary 128.0.2672.0 HOT 2
- Publishing to AMO failed
- [BUG] Sudden "Syntax Error" in Userscript HOT 1
- [BUG] GM_openInTab with option "insert" does not open in tab next to script tab HOT 5
- [Feature] Sort by last used HOT 2
- [Feature] make one userscript having both page part and isolated content part HOT 8
- How can I get the craft helper in infinite on iOS HOT 1
- [BUG] Uncaught Promise Error HOT 2
- [BUG] console.log wont print anything for @grant non-nones. HOT 3
- [Feature] Put old versions of scripts when updating into recycle bin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from violentmonkey.