Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name lepro.youtube
- // @author PsychodelEKS (11721)
- // @description Insert preview-link to youtube video.
- // @ujs:category site: enhancements
- // @ujs:published 2009-04-16 19:38
- // @ujs:modified 2009-04-16 23:56
- // @ujs:documentation n/a
- // @ujs:download n/a
- // @include http://*leprosorium.ru*
- // @namespace http://*leprosorium.ru*
- // ==/UserScript==
- // Version 1.0
- // - Initial
- // Version 1.1
- // - Big preview image added (first one), thx 2 qexy (5624)
- function createYoutubeLink(e)
- {
- var textarea = false;
- var toolbar = false;
- if ( window.location.href.indexOf("comments") != -1 || window.location.href.indexOf("inbox") != -1)
- {
- textarea = document.getElementById('comment_textarea');
- if(textarea)
- toolbar = textarea.parentNode.previousSibling.previousSibling;
- }
- if ( window.location.href.indexOf("write") != -1 )
- {
- textarea = document.getElementById('comments-textarea');
- toolbar = document.createElement('div');
- toolbar.style.marginBottom = '5px';
- textarea.parentNode.insertBefore(toolbar, textarea);
- }
- if( window.location.href.indexOf("asylum") != -1 )
- {
- textarea = document.getElementById('comment_textarea');
- toolbar = textarea.parentNode.parentNode.parentNode.rows[0].cells[0];
- }
- if ( textarea )
- {
- var youtube_button = document.createElement('a');
- youtube_button.href = 'javascript:void(0);';
- youtube_button.innerHTML = '<b>YouTube</b>';
- youtube_button.addEventListener('click', function()
- {
- var youtube_link = '';
- youtube_link = window.prompt('Ссылка на ролик:', '');
- var link_parser = /v=(.+?)(?:&|$)/im;
- var parsed_link = link_parser.exec(youtube_link);
- delete(link_parser);
- if(parsed_link != null && parsed_link[1])
- {
- var video_id = parsed_link[1];
- var preview_block = document.createElement('div');
- toolbar.appendChild(preview_block);
- for (var i = 0; i < 4; i++)
- {
- var tmp_preview_img = document.createElement('img');
- tmp_preview_img.src = 'http://i.ytimg.com/vi/'+video_id+'/'+i+'.jpg?'+(((new Date()).getTime()%10000000)*100+Math.round(Math.random()*99));
- tmp_preview_img.style.border = '2px solid white';
- tmp_preview_img.style.cssFloat = 'left';
- tmp_preview_img.width = 130;
- tmp_preview_img.addEventListener('mouseover', function(){ this.style.borderColor='limegreen'; }, false);
- tmp_preview_img.addEventListener('mouseout', function(){ this.style.borderColor='white'; }, false);
- tmp_preview_img.addEventListener('click',
- function()
- {
- var begin = textarea.value.substr(0, textarea.selectionStart);
- var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
- var end = textarea.value.substr(textarea.selectionEnd);
- var newCursorPos = textarea.selectionStart;
- var scrollPos = textarea.scrollTop;
- var insert_link = "\n"+'<a href="http://www.youtube.com/watch?v='+video_id+'"><img src="'+this.src+'" alt="YouTube video"></a>'+"\n";
- textarea.value = begin + insert_link + end;
- textarea.setSelectionRange(newCursorPos+insert_link.length+2, newCursorPos+insert_link.length+2);
- textarea.focus();
- toolbar.removeChild(preview_block);
- return false;
- }, false);
- /*tmp_preview_img.addEventListener('load',
- function()
- {
- this.title = "Размер: "+this.width+"x"+this.height;
- if(this.width > 130)
- this.width = 130;
- }, false);*/
- preview_block.appendChild(tmp_preview_img);
- }
- var close_button = document.createElement('div');
- close_button.innerHTML = '<b>X</b>';
- close_button.style.color = '#DEDEDE';
- close_button.style.cssFloat = 'left';
- close_button.addEventListener('mouseover', function(){ this.style.color='#AEAEAE'; }, false);
- close_button.addEventListener('mouseout', function(){ this.style.color='#DEDEDE'; }, false);
- close_button.addEventListener('click', function(){ toolbar.removeChild(preview_block); return false; }, false);
- preview_block.appendChild(close_button);
- }
- else if( youtube_link != '' && youtube_link != undefined )
- window.alert('Не разобрал ссылку. ('+youtube_link+')');
- return false;
- }, false);
- toolbar.appendChild(youtube_button);
- }
- }
- if( /opera/.test(navigator.userAgent.toLowerCase()) )
- document.addEventListener("DOMContentLoaded", function(e){ createYoutubeLink(); }, false);
- if( (/mozilla/.test(navigator.userAgent.toLowerCase()) && !/(compatible|webkit)/.test(navigator.userAgent.toLowerCase())) || /webkit/.test(navigator.userAgent.toLowerCase()) )
- createYoutubeLink();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement