youtube.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Reworks and improvements by Ivan Tcholakov, JUL-2009.
  2. var dialog = window.parent ;
  3. var oEditor = dialog.InnerDialogLoaded() ;
  4. var FCK = oEditor.FCK ;
  5. var FCKLang = oEditor.FCKLang ;
  6. var FCKConfig = oEditor.FCKConfig ;
  7. var FCKTools = oEditor.FCKTools ;
  8. // Security RegExp
  9. var REG_SCRIPT = new RegExp( "< *script.*>|< *style.*>|< *link.*>|< *body.*>", "i" ) ;
  10. var REG_PROTOCOL = new RegExp( "javascript:|vbscript:|about:", "i" ) ;
  11. var REG_CALL_SCRIPT = new RegExp( "&\{.*\};", "i" ) ;
  12. var REG_EVENT = new RegExp( "onError|onUnload|onBlur|onFocus|onClick|onMouseOver|onMouseOut|onSubmit|onReset|onChange|onSelect|onAbort", "i" ) ;
  13. // Cookie Basic
  14. var REG_AUTH = new RegExp( "document\.cookie|Microsoft\.XMLHTTP", "i" ) ;
  15. // TEXTAREA
  16. var REG_NEWLINE = new RegExp( "\x0d|\x0a", "i" ) ;
  17. var YoutubeSite = ('https:' == document.location.protocol ? 'https:' : 'http:') + '//www.youtube.com/v/';
  18. var HighQualityString = '%26hl=en%26fs=1%26rel=0%26ap=%2526fmt=18' ;
  19. var LowQualityString = '%26hl=en%26fs=1%26rel=0' ;
  20. // Set the language direction.
  21. window.document.dir = FCKLang.Dir ;
  22. FCKLang['DlgYouTubeURLTipContent1'] = FCKLang['DlgYouTubeURLTipContent1'] ? FCKLang['DlgYouTubeURLTipContent1'] : '' ;
  23. FCKLang['DlgYouTubeURLTipContent3'] = FCKLang['DlgYouTubeURLTipContent3'] ? FCKLang['DlgYouTubeURLTipContent3'] : '' ;
  24. FCKLang['DlgYouTubeURLTipContent1'] = FCKLang['DlgYouTubeURLTipContent1'].toString().replace( '%s', '<a href="http://www.youtube.com/" target="_blank">http://www.youtube.com/<\/a>' ) ;
  25. FCKLang['DlgYouTubeURLTipContent3'] = FCKLang['DlgYouTubeURLTipContent3'].toString().replace( '%s', 'http://www.youtube.com/watch?v=XXXXXXXXXXX...' ) ;
  26. //#### Dialog Tabs
  27. // Set the dialog tabs.
  28. dialog.AddTab( 'Info', FCKLang.DlgInfoTab ) ;
  29. dialog.AddTab( 'Preview', FCKLang.DlgImgPreview ) ;
  30. // This function is called when a dialog tab has been selected.
  31. function OnDialogTabChange( tabCode )
  32. {
  33. ShowE( 'divInfo', ( tabCode == 'Info' ) ) ;
  34. ShowE( 'divPreview', ( tabCode == 'Preview' ) ) ;
  35. if ( tabCode == 'Preview' )
  36. {
  37. UpdatePreview() ;
  38. }
  39. else
  40. {
  41. ClearPreview() ;
  42. }
  43. }
  44. // Get the selected video (if available).
  45. var oFakeImage = FCK.Selection.GetSelectedElement() ;
  46. var oEmbed ;
  47. if ( oFakeImage )
  48. {
  49. if ( oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute( '_fckvideo' ) )
  50. oEmbed = FCK.GetRealElement( oFakeImage ) ;
  51. else
  52. oFakeImage = null ;
  53. }
  54. window.onload = function()
  55. {
  56. // Translate the dialog box texts.
  57. oEditor.FCKLanguageManager.TranslatePage(document) ;
  58. // Load the selected element information (if any).
  59. LoadSelection() ;
  60. dialog.SetAutoSize( true ) ;
  61. // Activate the "OK" button.
  62. dialog.SetOkButton( true ) ;
  63. SelectField( 'txtUrl' ) ;
  64. }
  65. function LoadSelection()
  66. {
  67. if ( ! oEmbed ) return ;
  68. var src = GetAttribute( oEmbed, 'src', '' ) ;
  69. GetE( 'txtUrl' ).value = GetOriginalYoutubeUrl ( src ) ;
  70. if ( GetQuality ( src ) == 'high' )
  71. {
  72. GetE( 'radioHigh' ).checked = true ;
  73. GetE( 'radioLow' ).checked = false ;
  74. }
  75. else
  76. {
  77. GetE( 'radioHigh' ).checked = false ;
  78. GetE( 'radioLow' ).checked = true ;
  79. }
  80. GetE( 'txtWidth' ).value = GetAttribute( oEmbed, 'width', 425 ) ;
  81. GetE( 'txtHeight' ).value = GetAttribute( oEmbed, 'height', 344 ) ;
  82. }
  83. //#### The OK button was hit.
  84. function Ok()
  85. {
  86. GetE( 'txtUrl' ).value = GetE( 'txtUrl' ).value.Trim() ;
  87. if ( GetE('txtUrl').value.length == 0 )
  88. {
  89. dialog.SetSelectedTab( 'Info' ) ;
  90. GetE( 'txtUrl' ).focus() ;
  91. alert( oEditor.FCKLang.DlgYouTubeCode ) ;
  92. return false ;
  93. }
  94. // Check security
  95. if ( !checkCode( GetE( 'txtUrl' ).value ) )
  96. {
  97. alert( oEditor.FCKLang.DlgYouTubeSecurity ) ;
  98. return false ;
  99. }
  100. oEditor.FCKUndo.SaveUndoStep() ;
  101. if ( !oEmbed )
  102. {
  103. oEmbed = FCK.EditorDocument.createElement( 'EMBED' ) ;
  104. oFakeImage = null ;
  105. }
  106. UpdateEmbed( oEmbed ) ;
  107. if ( !oFakeImage )
  108. {
  109. oFakeImage = oEditor.FCKDocumentProcessor_CreateFakeImage( 'FCK__Video', oEmbed ) ;
  110. oFakeImage.setAttribute( '_fckvideo', 'true', 0 ) ;
  111. oFakeImage = FCK.InsertElement( oFakeImage ) ;
  112. }
  113. oEditor.FCKEmbedAndObjectProcessor.RefreshView( oFakeImage, oEmbed ) ;
  114. return true ;
  115. }
  116. function UpdateEmbed( e )
  117. {
  118. var YoutubeUrl = GetE( 'txtUrl' ).value ;
  119. var YoutubeId = GetYoutubeId( YoutubeUrl ) ;
  120. SetAttribute( e, 'type', 'application/x-shockwave-flash' ) ;
  121. SetAttribute( e, 'pluginspage', 'http://www.macromedia.com/go/getflashplayer' ) ;
  122. SetAttribute( e, 'allowfullscreen', 'true' ) ;
  123. SetAttribute( e, 'wmode', 'transparent' ) ;
  124. if ( GetE( 'radioHigh' ).checked )
  125. {
  126. SetAttribute( e, 'src', YoutubeSite + YoutubeId + HighQualityString ) ;
  127. }
  128. else
  129. {
  130. SetAttribute( e, 'src', YoutubeSite + YoutubeId + LowQualityString ) ;
  131. }
  132. SetAttribute( e, 'width' , GetE( 'txtWidth' ).value == '' ? 425 : GetE( 'txtWidth' ).value ) ;
  133. SetAttribute( e, 'height', GetE( 'txtHeight' ).value == '' ? 344 : GetE( 'txtHeight' ).value ) ;
  134. }
  135. function checkCode( code )
  136. {
  137. if ( code.search( REG_SCRIPT ) != -1 )
  138. {
  139. return false ;
  140. }
  141. if ( code.search( REG_PROTOCOL ) != -1 )
  142. {
  143. return false ;
  144. }
  145. if ( code.search( REG_CALL_SCRIPT ) != -1 )
  146. {
  147. return false ;
  148. }
  149. if ( code.search( REG_EVENT ) != -1 )
  150. {
  151. return false ;
  152. }
  153. if ( code.search( REG_AUTH ) != -1 )
  154. {
  155. return false ;
  156. }
  157. if ( code.search( REG_NEWLINE ) != -1 )
  158. {
  159. return false ;
  160. }
  161. return true ;
  162. }
  163. function GetOriginalYoutubeUrl ( url )
  164. {
  165. var end = url.indexOf( '%' ) ;
  166. if ( end > 0 )
  167. {
  168. url = url.substring( 0, end ) ;
  169. }
  170. url = url.replace( '/v/', '/watch?v=' ) ;
  171. return url ;
  172. }
  173. function GetYoutubeId( url )
  174. {
  175. var YoutubeId = url.toString().slice( url.search( /\?v=/i ) + 3 ) ;
  176. var end = YoutubeId.indexOf( '%' ) ;
  177. if ( end > 0 )
  178. {
  179. YoutubeId = YoutubeId.substring( 0, end ) ;
  180. }
  181. return YoutubeId ;
  182. }
  183. function GetQuality ( url )
  184. {
  185. var quality = 'low' ;
  186. var QualityString = url.toString().substr( url.search( '%' ) ) ;
  187. if ( QualityString.length > LowQualityString.length )
  188. {
  189. quality = 'high' ;
  190. }
  191. return quality ;
  192. }
  193. var ePreview ;
  194. function IsValidMedia( e )
  195. {
  196. if ( !e )
  197. return false ;
  198. var src = GetAttribute( e, 'src', '' ) ;
  199. var width = GetAttribute( e, 'width', '' ) ;
  200. var height = GetAttribute( e, 'height', '' ) ;
  201. if ( src.length == 0 )
  202. return false ;
  203. if ( src.toString().toLowerCase().indexOf( 'youtube.com/v/%' ) != -1 )
  204. return false ;
  205. if ( isNaN( width ) )
  206. return false ;
  207. if ( parseInt( width, 10 ) <= 0 )
  208. return false ;
  209. if ( isNaN( height ) )
  210. return false ;
  211. if ( parseInt( height, 10 ) <= 0 )
  212. return false ;
  213. return true ;
  214. }
  215. function SetPreviewElement( previewEl )
  216. {
  217. ePreview = previewEl ;
  218. if ( IsValidMedia( oEmbed ) )
  219. UpdatePreview() ;
  220. }
  221. function UpdatePreview()
  222. {
  223. if ( !ePreview )
  224. return ;
  225. while ( ePreview.firstChild )
  226. ePreview.removeChild( ePreview.firstChild ) ;
  227. var oDoc = ePreview.ownerDocument || ePreview.document ;
  228. var e = oDoc.createElement( 'EMBED' ) ;
  229. UpdateEmbed( e ) ;
  230. if ( !IsValidMedia( e ) )
  231. {
  232. ClearPreview() ;
  233. }
  234. else
  235. {
  236. var max_width = 515 ;
  237. var max_height = 275 ;
  238. var width = GetAttribute( e, 'width', 425 ) ;
  239. var height = GetAttribute( e, 'height', 344 ) ;
  240. var new_size = FCK.ResizeToFit( width, height, max_width, max_height ) ;
  241. width = new_size[0] ;
  242. height = new_size[1] ;
  243. SetAttribute( e, 'width' , width ) ;
  244. SetAttribute( e, 'height', height ) ;
  245. ePreview.appendChild( e ) ;
  246. var margin_left = parseInt( ( max_width - width ) / 2, 10 ) ;
  247. var margin_top = parseInt( ( max_height - height ) / 2, 10 ) ;
  248. if ( ePreview.currentStyle )
  249. {
  250. // IE
  251. ePreview.style.marginLeft = margin_left ;
  252. ePreview.style.marginTop = margin_top ;
  253. }
  254. else
  255. {
  256. // Other browsers
  257. SetAttribute( ePreview, 'style', 'margin-left: ' + margin_left + 'px; margin-top: ' + margin_top + 'px;' ) ;
  258. }
  259. }
  260. }
  261. function ClearPreview()
  262. {
  263. if ( !ePreview )
  264. return ;
  265. while ( ePreview.firstChild )
  266. ePreview.removeChild( ePreview.firstChild ) ;
  267. ePreview.innerHTML = '&nbsp;' ;
  268. }