Tieba_Insert_MusicLink

贴吧自定义插入mp3链接

  1. // ==UserScript==
  2. // @name Tieba_Insert_MusicLink
  3. // @namespace tieba
  4. // @description 贴吧自定义插入mp3链接
  5. // @include http://tieba.baidu.com/*
  6. // @require http://libs.baidu.com/jquery/1.9.0/jquery.js
  7. // @grant GM_addStyle
  8. // @version 3.0.2
  9. // @author 初代作者为校长之怒/修复(绯色)
  10. // ==/UserScript==
  11.  
  12. /***
  13. 2014年之前版本信息丢失,且本脚本真正的作者早已无更新
  14. 2014/6/1 修复火狐吧友说该脚本在chrome的浮动回帖框会出现两个插入框的问题(在此吐槽,狐吧基佬跑去使用chrome闹哪样)
  15. */
  16. //JQuery支持
  17.  
  18. //音乐链接面板 GUI界面
  19. var musicUrl = '<span class="label">歌名</span><input style="width:100px" id="musicUrlTitle" placeholder="输入歌曲名" value="">';
  20. musicUrl += '&nbsp;<span class="label">链接</span><input style="width:300px" id="musicUrl" placeholder="输入链接" value="">';
  21. musicUrl += '&nbsp;<span id="convertLinks" class="btn" >插入音乐链接</span><span id="Check_status" style="color:red;display:none">歌名和链接不能为空</span>';
  22. //css
  23. var css = '.label{display:inline-block;padding:2px 4px;font-size:11.844px;font-weight:bold;line-height:14px;color:#ffffff;vertical-align:baseline;white-space:nowrap;text-shadow:0 -1px 0 rgba(0, 0, 0, 0.25);background-color:#999999;}\
  24. .btn{font-size: 12px;height: 25.6px;line-height: 25.6px;padding: 0px 2px;transition-property: #000, color;\
  25. transition-duration: 0.3s;\
  26. box-shadow: none;\
  27. background: none repeat scroll 0% 0% #00A1CB;\
  28. color: #FFF;\
  29. text-shadow: none;\
  30. border: medium none;}';
  31. GM_addStyle(css);
  32.  
  33. //调用控制
  34. addNodeInsertedListener('#tb_rich_poster_container', function() {
  35. $("#tb_rich_poster").append(musicUrl);
  36. $('#convertLinks').click(check);
  37. });
  38.  
  39. //函数 检查表单
  40.  
  41. function check() {
  42. if (($('#musicUrlTitle').val() == '') || ($('#musicUrl').val() == '')) {
  43. $('#Check_status').css('display', 'block');
  44. setTimeout(function() {
  45. $('#Check_status').hide();
  46. }, 2000);
  47. } else {
  48. location.href = "javascript:rewriteGetContent();void(0);";
  49. convertLinks();
  50. }
  51. }
  52. //插入函数 插入音乐
  53.  
  54. function convertLinks() {
  55. var url = $('#musicUrl').val();
  56. $('#musicUrl').val('');
  57. var title = $('#musicUrlTitle').val();
  58. if (url.indexOf('http://') == -1 && url.indexOf('https://') == -1 && url.indexOf('ftp://') == -1)
  59. url = 'http://' + url;
  60. var temp = '<img data-height="95" data-width="400"';
  61. temp += ' title="http://box.baidu.com/widget/flash/bdspacesong.swf?from=tiebasongwidget&amp;url=';
  62. temp += url;
  63. temp += '&amp;name=' + encodeURIComponent(title) + '&amp;artist=';
  64. temp += '&amp;extra=&amp;autoPlay=false&amp;loop=true"';
  65. temp += 'src="http://tieba.baidu.com/tb/editor/v2/music.png" class="BDE_Music">';
  66. $("#ueditor_replace").html($('#ueditor_replace').html() + temp);
  67. $('.dialogJ,.dialogJmodal').remove();
  68. }
  69.  
  70. //函数 元素精确定位
  71.  
  72. function addNodeInsertedListener(elCssPath, handler, executeOnce, noStyle) {
  73. var animName = "anilanim",
  74. prefixList = ["-o-", "-ms-", "-khtml-", "-moz-", "-webkit-", ""],
  75. eventTypeList = ["animationstart", "webkitAnimationStart", "MSAnimationStart", "oAnimationStart"],
  76. forEach = function(array, func) {
  77. for (var i = 0, l = array.length; i < l; i++) {
  78. func(array[i]);
  79. }
  80. };
  81. if (!noStyle) {
  82. var css = elCssPath + "{",
  83. css2 = "";
  84. forEach(prefixList, function(prefix) {
  85. css += prefix + "animation-duration:.001s;" + prefix + "animation-name:" + animName + ";";
  86. css2 += "@" + prefix + "keyframes " + animName + "{from{opacity:.9;}to{opacity:1;}}";
  87. });
  88. css += "}" + css2;
  89. GM_addStyle(css);
  90. }
  91. if (handler) {
  92. var bindedFunc = function(e) {
  93. var els = document.querySelectorAll(elCssPath),
  94. tar = e.target,
  95. match = false;
  96. if (els.length !== 0) {
  97. forEach(els, function(el) {
  98. if (tar === el) {
  99. if (executeOnce) {
  100. removeNodeInsertedListener(bindedFunc);
  101. }
  102. handler.call(tar, e);
  103. return;
  104. }
  105. });
  106. }
  107. };
  108. forEach(eventTypeList, function(eventType) {
  109. document.addEventListener(eventType, bindedFunc, false);
  110. });
  111. return bindedFunc;
  112. }
  113. }
  114. //函数 元素精确定位取消绑定
  115.  
  116. function removeNodeInsertedListener(bindedFunc) {
  117. var eventTypeList = ["animationstart", "webkitAnimationStart", "MSAnimationStart", "oAnimationStart"],
  118. forEach = function(array, func) {
  119. for (var i = 0, l = array.length; i < l; i++) {
  120. func(array[i]);
  121. }
  122. };
  123. forEach(eventTypeList, function(eventType) {
  124. document.removeEventListener(eventType, bindedFunc, false);
  125. });
  126. }
  127.  
  128. //度娘处理函数改写(来自猫酱和小鹿姐)
  129.  
  130. function rewriteGetContent() {
  131. var b = test_editor.getContent;
  132. test_editor.getContent = function() {
  133. cr_flash = [];
  134. var d = b.call(test_editor);
  135. d = d.replace(/&#39;/g, "'").replace(/&quot;/g, '"').replace(/(^(<br\/>)+)|((<br\/>)+$)/g, "");
  136. var embeds = d.match(/<embed[^>]*>/g);
  137. if (embeds) {
  138. var f = '<embed allowfullscreen="true" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" scale="noborder" src="#{url}" class="BDE_Music" width="400" height="95"/>';
  139. $('#ueditor_replace .BDE_Music').each(function() {
  140. var g = $.tb.format(f, {
  141. url: $(this).attr('title')
  142. });
  143. cr_flash.push(g);
  144. });
  145. for (var i = 0; i < embeds.length; i++)
  146. d = d.replace(embeds[i], cr_flash[i]);
  147. }
  148. return d;
  149. };
  150. }
  151. location.href = "javascript:"+String(rewriteGetContent) + ";void(0)";