episode evaluation

give evaluation to episode

  1. // ==UserScript==
  2. // @name episode evaluation
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.3
  5. // @description give evaluation to episode
  6. // @author xdy
  7. // @include /http:\/\/(bgm\.tv|bangumi\.tv|chii\.in)/ep/\d+$
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. function addCSS(cssText){
  12.  
  13. var style = document.createElement('style'), //创建一个style元素
  14. head = document.head || document.getElementsByTagName('head')[0]; //获取head元素
  15. style.type = 'text/css'; //这里必须显示设置style元素的type属性为text/css,否则在ie中不起作用
  16.  
  17. if(style.styleSheet){ //IE
  18. var func = function(){
  19. try{ //防止IE中stylesheet数量超过限制而发生错误
  20.  
  21. style.styleSheet.cssText = cssText;
  22.  
  23. }catch(e){
  24.  
  25. }
  26. };
  27. //如果当前styleSheet还不能用,则放到异步中则行
  28. if(style.styleSheet.disabled){
  29.  
  30. setTimeout(func,10);
  31. }else{
  32. func();
  33. }
  34. }else{ //w3c
  35. //w3c浏览器中只要创建文本节点插入到style元素中就行了
  36. var textNode = document.createTextNode(cssText);
  37.  
  38. style.appendChild(textNode);
  39.  
  40. }
  41.  
  42. head.appendChild(style); //把创建的style元素插入到head中
  43. }
  44.  
  45. mycss = '.inputButton {background-color: #F09199;color: #fff; cursor: pointer;font-family: lucida grande,tahoma,verdana,arial,sans-serif;font-size: 11px;padding: 1px 3px;text-decoration: none;}'+
  46. '.forum_category{background-color:#F09199;color:#fff;font-weight:700;padding:3px;}'+
  47. '.vote_container{background-color:#e1e7f5}'+
  48. '.forum_boardrow1{background-color:#fff;border-color:#ebebeb;border-style:solid;border-width:0;padding:6px 4px;vertical-align:top}';
  49.  
  50. (function() {
  51. var islogin = !$("div").is(".guest");
  52. if(islogin)
  53. {
  54. var url = location.pathname;
  55. url = url.split('/');
  56. var ep_id = url[2];
  57. var info = $('div.idBadgerNeue a.avatar').attr('href');
  58. info = info.split('/');
  59. var user_id = info[4];
  60. var name = $('div#headerSubject a').html();
  61. var ep = $('div#columnEpA h2.title').html();
  62. ep = ep.split(' ')[0];
  63. //var myurl = "http://127.0.0.1:8000/bgm/";
  64. var myurl = "http://39.106.26.175:8000/bgm/";
  65. addCSS(mycss);
  66. $.ajax({
  67. url: myurl+"user_ep",
  68. jsonp: "callback",
  69. dataType: "jsonp",
  70. data: {
  71. ep_id:ep_id,
  72. user_id:user_id,
  73. },
  74. success: function( ret ) {
  75.  
  76. if(!ret.res){
  77. var html = '<div id="poll_container" style="width:670px">' +
  78. '<div class="forum_category">'+name+' '+ep+'观感</div>'+
  79. '<div class="forum_boardrow1" style="border-width: 0 1px 1px 1px;">'+
  80. '<div style="margin: 3px 0;"><label><input type="radio" name="pollOption" value="5"> 5/5 超棒!</label></div>'+
  81. '<div style="margin: 3px 0;"><label><input type="radio" name="pollOption" value="4"> 4/5 不错</label></div>'+
  82. '<div style="margin: 3px 0;"><label><input type="radio" name="pollOption" value="3"> 3/5 一般</label></div>'+
  83. '<div style="margin: 3px 0;"><label><input type="radio" name="pollOption" value="2"> 2/5 不喜欢</label></div>'+
  84. '<div style="margin: 3px 0;"><label><input type="radio" name="pollOption" value="1"> 1/5 厌恶</label></div>'+
  85. '<br><input type="submit" name="voteButton" value="投票" class="inputButton" id="voteButton"></div></div>';
  86. $("#columnEpA").find("[class='epDesc']").append(html);
  87. var button = document.getElementById('voteButton');
  88. button.onclick = function(){
  89. var val = $('input:radio:checked').val();
  90. if(val == undefined){
  91. alert("请选择后再投票!");
  92. return;
  93. }
  94. $.ajax({
  95. url: myurl+"addVote",
  96. jsonp: "callback",
  97. dataType: "jsonp",
  98. data: {
  99. ep_id:ep_id,
  100. user_id:user_id,
  101. rate:val
  102. },
  103. success: function( ret ) {
  104. if(ret.success){
  105. info = getInfo(ret);
  106. $('div#poll_container').html(info);
  107. }
  108. else{
  109. alert(ret.message);
  110. }
  111. }
  112. });
  113. };
  114. }
  115.  
  116. else{
  117. var html = '<div id="poll_container" style="width:670px">' + getInfo(ret) + '</div>';
  118. $("#columnEpA").find("[class='epDesc']").append(html);
  119. }
  120. ; }
  121. });
  122.  
  123. }
  124. })();
  125.  
  126. function getInfo(ret){
  127. var judge_str = ['5/5 很棒!', '4/5 不错', '3/5 一般', '2/5 不喜欢', '1/5 厌恶'];
  128. var html = '<div class="forum_category">投票结果</div><div class="forum_boardrow1" style="border-width: 0 1px 1px 1px;">'+
  129. '<table border="0" width="100%" cellpadding="" cellspacing="5">';
  130. for(var i=0;i<5;i++)
  131. {
  132. html = html + '<tr><td align="left">'+judge_str[i];
  133. if(ret.choice == 5-i)
  134. {
  135. html += '<small>(your vote)</small>';
  136. }
  137. html = html + '</td><td width="35%"><div class="vote_container" style="width: ' + ret.width[i] + '%">&nbsp;</div></td><td width="25" align="center">';
  138. html = html + ret.count[i] + '</td><td width="40" align="right">' + ret.width[i] + '%</td></tr>';
  139. }
  140. html = html + '</table><div style="text-align: center;">Voters: ' + ret.voters + '</div></div>';
  141. return html;
  142. }
  143.