Greasemonkey with Youtube - javascript

So, I decided that starting small with learning either Javascript or Greasemonkey one at a time was just not ambitious or efficient enough. So I decided to make a Greasemonkey script that alters the way Youtube works.
I'll start with problems I am pretty sure it isn't, I do have the HTML5 player enabled, and I double checked the names of the functions I am calling in the Youtube API. I am also pretty sure that I installed Greasemonkey correctly and that the script is loading correctly, based on the fact that when a Youtube page is open, the "YoutubeSpeed.user.js" file is in the Greasemonkey context menu with a check mark next to it, and that I tried installing a much simpler Greasemonkey script (http://www.wired.com/wired/archive/webmonkey/greasemonkey/helloworld.user.js) and it worked just fine. In case it isn't obvious, I am basically trying to have Youtube do a bunch of stuff as soon as a video is opened. (specifically, have it pause, set the speed to 2x, set the quality to 720p, and make sure the volume is at 100.
I think the problem is either with my use of the Youtube API, or my use of Javascipt syntax, or my use of Greasemonkey syntax. I really think these could all be useful tools going forward, so a little help learning them would be much appreciated. Also, any other ideas for improving the general Youtube experience to add to this script would be appreciated as well.
EDIT: Ah, it seems there was a mistype. I absolutely meant to include my script, and since it's pretty short, I hope you guys will be able to help me. Here it is in all it's glory.
// ==UserScript==
// #name YoutubeSpeed
// #namespace matt47721#yahoo.com
// #description Double The Speed Of Youtube Videos By Default
// #include https://www.youtube.com/*
function onPlayerReady() {
target.player.pauseVideo();
target.player.setPlaybackRate(2);
target.player.setPlaybackQuality(hd720);
target.player.unMute();
target.player.setVolume(volume:100);
};
// #grant none
// ==/UserScript==
As to an MCVE, for M, I have been unable to make any Greasemonkey script involving Youtube videos work. As for the C, the entirety of my script is there. And as for the V, I guess I could have described that a little better, my apologies. When I open a Youtube video, it does none of the things I am trying to accomplish with this script, it just plays normally. And since the question is about how to make my Greasemonkey script work with Youtube, and the only code I put in is the Greasemonkey script that is supposed to affect Youtube I would say I've eliminated things not relevant to the problem.
I hope this is satisfactory, I apologize for doing so poorly before, this is my first time using StackOverflow.
Cheers,
Eridine

Your script references a variable "target.player" but you haven't linked that variable to the player on the youtube page. You need to first find the player using something like:
var videoElement = document.getElementsByTagName("video")[0];
which gives you a handle to the video player, only then can you manipulate the player using the API. A complete, working, script can be found here:
https://github.com/mirnhoj/video-element-playbackrate-setter/

Related

My sounds are not playing in Chrome

I have a small, very basic 3d game that runs in all browsers. But my issue is that when it runs in Chrome I can see in my console messages that the sounds are "pending" and don't play when they should. The thing is that after some time have passed ALL sounds play at the same time. It doesn't seem to be happening anywhere else, its just in Chrome. I came across this article that someone posted but it did not solve my issue. Just in case I am running Version 43.0.2357.81 m of Chrome and I am using the Sound Manager 2 library (I needed it in order to have sound work with IE 9 and up).
Can anyone offer some suggestions or point me to any more articles that might point me to a solution? Many thanks in advance!
Turn on logging in Chrome (inspect element/console - Preserve log) and see what your code is doing.
Also, you could try enabling/disabling your audio flags just be sure to set them back to default when you are done.
chrome://flags/#disable-encrypted-media
chrome://flags/#disable-prefixed-encrypted-media/
chrome://flags/#try-supported-channel-layouts
chrome://flags/#enable-delay-agnostic-aec
chrome://flags/#disable-delay-agnostic-aec
chrome://flags/#enable-tab-audio-muting
in chrome and see if that makes a difference.
Just shooting from the hip on the information given though, I would imagine that chrome is delaying your audio javascript execution to the end of the page load. Moving the script to the beginning of the page, or using an async could fix this. Here is a good resource for javascript page load issues: http://www.html5rocks.com/en/tutorials/speed/script-loading/

Youtube, the site itself freezing for a few seconds

i've been plagued by a problem that seem to started since Youtube's last
layout change this year.
I have for months put up and tried to ignore the freezing that happens
everytime I open a link within youtube such as an channel, an video, the homepage, etc.
I'm using Chrome version 31.0.1650.63 and I have already tested youtube without any
extensions, cookies, cache, etc but it still affects me to this day and maybe some
of you out there.
I searched every where for the solution and couldn't find any anywhere.
I did a bit of digging and testing to see what is the root of the problem and
while i'm no expertise on code or Java...I did found something.
Every time I reload a page on youtube, with it finishing loading up...this happens:
http://i.imgur.com/K9Z3vJ1.png
See that?
The purple-colored rendering, that's the cause of the lag every time I load
up something on youtube!
I notice that it repeats the same thing over and over again for like 5-15 or so
seconds before it starts loading up the thumbnails of the videos.
If you want a closer inspection of the code, here's the raw data from the timeline (that you can load up in chrome's dev timeline window):
Youtube Lag JSON Timeline file
I don't what to do with this problem and I believe it affects
me in Firefox as well but to an lesser extent then chrome.
Do any of you guys know how to resolve this problem?
Honestly I doubt that the youtube devs will fix this problem since they're the ones who caused it in the first place. (Like not being able to subscribe to some people when i'm not even at my sub limit.)
I'm sorry if you don't want to help but I don't know who else to look to besides
the experts that y'all are when it comes to web coding. If anything, I believe a userscript or something similar could help with this but how...I don't know.
I'm new to this and I apologize if the question i'm asking isn't suited for this site.
Still, thanks for reading and if you can...please reply.
That is all and thanks again.
Well I was fiddling around and looking at the source code
with chromes timeline devtools and well...
I found a temporary solution to the problem which seems to have
fixed and ended the freezing on youtube! yay~
But some things don't work with this solution and i'm at
a fault with this issue after some hours trying to figure out
how to stop the problem.
Anyways I used adblock, an extension that I have on Google Chrome, to deal
with this issue and was able to get everything fixed and working!
What I did was, in the custom filters section (check options in your
adblock extension, you should have it.) and added the following then did
some testing to see if it fixed the problem which it did!
Here are the filters I used to fix it!
||s.ytimg.com/yts/jsbin/www-pageframe-*
*www-pageframe-*.js
^^^
Yeah, that's the temporary solution as it fixes the problems but it causes the following...
You can't open the menu which contains links to your playlists, user settings, channel, etc when you click your username or profile
picture on top right of youtube.
The "..." mini-menu that you usually find on the right of a video section (to hide, subscribe, etc.) doesn't appear anymore.
Youtube's quick menu button doesn't work, however the menu is still open but you won't be able to close it. That or it's close and you
won't be able to open it...I guess.
Those are the problems I found with this temporarily solution to the freezing problem.
Yeah...but I hope this helps anyone who's having this problem somewhat well.
But it would be nice if someone has an better solution then mine hehe!
Thanks for reading and if you have a better solution, don't feel afraid to
tell! ^_^
I'll be awaiting other answers, for now...I hope this helps y'all well
in some ways hehe! =D
This certainly isn't javascript, but substituting the normal EasyList in ABP for EasyList without element hiding will fix the issue. There are downsides to this as well but for now seems to be the better solution.

Refresh selected iframes on browser resize

I'm a complete beginner in the development side of things and would greatly appreciate any help.
I have the following code that refreshes an entire page when the browser is resized:
$(window).bind('resize',function(){
window.location.href = window.location.href;
});
However, I want the refresh to be targeted at particular iframes with unique id's instead of the whole page.
Again, thank you in advance for any help.
Use:
$(window).resize(function(){
document.getElementById('FrameID').contentDocument.location.reload(true);
});
And consider using classes instead of IDs if you have multiple iFrames.
I am having issues with the Facebook social widget and I was hoping this would fix it. Your code was helpful (a good start).
That said, it did not work.
I will post a solution if I find one. Facebook really needs to update their widget. It really is problematic with responsive sites (mobile devices). Considering they created React code, it really is not very well written.
jQuery seems a better solution since I did not have an id to target) but the problem is deeper within the iframe.
// refresh facebook iframe on page resize (did not work)
$(window).resize(function(){
$('.fb iframe')[0].contentDocument.location.reload(true);
});
//--- other ways to reload iframes---
//reload 1 iframe
$('#iframe')[0].contentWindow.location.reload(true);
//reload all iFrames
$('iframe').each(function() {
this.contentWindow.location.reload(true);
});
//Another way to reload all iFrames
$('iframe').attr('src', $('iframe').attr('src'));
Update...
I found these two well-written pages on the subject. Hopefully, it works.
How to make the Facebook Page Plugin fully responsive
Making The Facebook Page Plugin Responsive
UPDATE
The code above is nice, but they did not have it ready to rock and roll. The showed examples for their site and I almost got it working, but there is still a bug in my code.
so...
This is totally cheating, but I was looking at the source code and opened the iframe location from Facebook and it actually worked (it seems allowed). So far it seems to work with other profiles too (so long as you grant permission first).
I just hardcoded the iframe vs using their JavaScript code. It also works 1000x times better/faster this way and it is already responsive. It is nuts I had spent so much time with trying to fix Facebook's code. Using a straight URL link in the iframe loads way faster (geezzz).
Here is an example of what I used for the final URL (I cleaned up the unneeded parameters.
https://www.facebook.com/v2.12/plugins/page.php?adapt_container_width=false&height=&hide_cover=true&href=https%3A%2F%2Fwww.facebook.com%2FYOURFACEBOOKUSERNAME&locale=en_US&show_facepile=false&small_header=true&tabs=timeline&width=500
Again, keep in mind for this to work, you need to create a social box at Facebook first, so that you can grant permission. I tried to do it with my personal username and since I have never created one before, it did not work. Again, this is theory, but that makes sense.My actual site is using another client and I also tested other clients. It seems to work fine.
No promises it will work on a live site. It is possible Facbook blocks users that do not use it via the script, but we will see. I am guessing Facebook may use scripts in the code to check for location source, so this way may fail over time (don't know yet). Also, it is possible I am missing a needed key that Facebook genarates from the script thatI failed to notice (or removed). I was careful when I cleaned it up, and I did not want to include more than needed. We will see.
Anyways, try your own tests via the source code please. I need to work on other more important stuff, and this works and seems to be a simple very effective solution. I will have more time later if it fails. The client wont know and again, it is faster and seem to work better.
<iframe id="fb-iframe" src="https://www.facebook.com/v2.12/plugins/page.php?adapt_container_width=false&height=&hide_cover=true&href=https%3A%2F%2Fwww.facebook.com%2FInternetBuilderConsulting&locale=en_US&show_facepile=false&small_header=true&tabs=timeline&width=500" width="100%" height="100%" allowtransparency="1" scrolling="no" frameborder="0"></iframe>
ONE MORE UPDATE.
It seems to be working, but I still need to send a refresh to the URL in the paramaters for the width. Also, note the maxium width for the social widget is 500px (so if you have a need for one bigger, it wont go more unless you use some kind of transform in CSS).
Here is the final code.
Funny about all this, I am back full circle. I am using some of the code we talked about above and some new code I figured out and wrote.
:)
Also noteworthy, I noticed the StackOverflow console is giving me an error.
Please ignore it. The error has something to do with the iframe reference calling Facebook which StackOverflow does not like. As far as I can tell, there is no error.
If I am wrong, let me know.
// Fixes Facebook iFrame on page resize (Responsive)
$(window).resize(function() {
$('#fb-iframe').attr('src', "https://www.facebook.com/v2.12/plugins/page.php?adapt_container_width=false&height=&hide_cover=true&href=https%3A%2F%2Fwww.facebook.com%2FInternetBuilderConsulting&locale=en_US&show_facepile=false&small_header=true&tabs=timeline&width=" + $('#fb-iframe').width())
});
<script language="JavaScript" type="text/javascript" src="/js/jquery-1.2.6.min.js"></script>
<iframe id="fb-iframe" src="https://www.facebook.com/v2.12/plugins/page.php?adapt_container_width=false&height=&hide_cover=true&href=https%3A%2F%2Fwww.facebook.com%2FInternetBuilderConsulting&locale=en_US&show_facepile=false&small_header=true&tabs=timeline&width=500" width="100%" height="100%" allowtransparency="1" scrolling="no" frameborder="0"></iframe>

HTML5 embed tag - event attributes not firing (onended, onpause etc)

I've searched for many hours trying to find a solution to this problem. I have a HTML5 web page with a background sound file that plays automatically when the page loads, and I'm trying to get a function to run when the sound file finishes playing. I've isolated the code I'm using into a test file to ensure nothing else is causing the problem. The sound plays fine in Firefox (which I'm using to test). From the many solutions I've tried, it seems as though the event attributes in the embed tag simply aren't firing at all.
Here is the test HTML page I'm using (tried putting HTML in here but it wouldn't show up properly, I know it's probably a noob error but I'm in a hurry with this):
Testpage
As you can see the message function works fine, as the "TEST" link indicates when clicked. But the messages for the media events don't appear.
If anyone can help me with this, I'd be most grateful as it's for a time-critical company project. I'd be happy to try alternative solutions to the event attributes, as long as I can get my own Javascript function to run on the sound file reaching its end, I'll be happy!!
Thanks!
Billy
Since you're using HTML5, it would be better to use the <audio> tag rather than <embed>. This works fine in my example:
http://jsfiddle.net/u6dbV/
You can include HTML here, but it has to be marked up as code or it'll get stripped out.

Is there a way to embed an mp3 player in a website that isn't flash based (so that the website is iPhone OS compatible)

I did a lot of searching for what I thought would be a pretty common question, but I came up with nothing. If there is another thread with a similar topic, please let me know.
Basically, I'm looking for a way to have an .mp3 file play in a website without relying on a flash-based player. I've searched w3 schools and every forum I can think of, but every media player I've found so far has been some sort of proprietary flash player.
Doesn't HTML support some sort of native player? I've found some that rely on Windows Media Player which is close, but I want the player to work on an iPhone and something tells me WMP won't get that done...
PS, as I'm thinking more about this this idea just popped into my head: a javascipt player and inside the <noscript> tag, put a flash player? I'm running a music blog (# http://www.freshoncampus.com) so the less code per post, the better...
Yes you can, with HTML 5.
This is a pretty good explanation of how you might go about doing this.
The caveat is that HTML5 support is not universal, but iOS devices (iphone) have a good start with supporting HTML5.
Edited to add:
From the question, it's hard to discern if you're looking for a way to play multiple mp3's with a nice gui interface, or just use audio as a background.
For the former, you will need to use Javascript to handle controls, and loading of the src element (I'd search for custom built javascript or jquery plugins to handle this).
For the latter, my solution above will work.
Also, background music in a webpage is highly annoying to most users, so caveat emptor.
You could go with something like http://www.schillmania.com/projects/soundmanager2/
which should autodetect the best option to play the sound.
jplayer? Not sure about MP3/OGG thing though...
First, HTML5's “audio” tag.
Second, you can use “embed” tag — it will play with whatever browser plugin is installed (not just WMP).
Not sure what would work in iPhone, though.
(and I might be wrong about exact tag names)

Categories

Resources