IMDB Copy Buttons V2

Buttons

当前为 2018-12-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name IMDB Copy Buttons V2
  3. // @namespace http://kmcgurty.com/
  4. // @version 1.2
  5. // @description Buttons
  6. // @author Kmcgurty
  7. // @match https://www.imdb.com/title/*
  8. // @match https://www.imdb.com/name/*
  9. // @connect omdbapi.com
  10. // @grant GM_addStyle
  11. // @grant GM_setClipboard
  12. // @grant GM_xmlhttpRequest
  13. // ==/UserScript==
  14.  
  15. //~README~
  16. //
  17. //to use the "Reveal original titles" feature, you need to get an API key from OMDB.
  18. //you can get that from here: http://www.omdbapi.com/apikey.aspx
  19. //select "FREE! (1,000 daily limit)"
  20. //use is "For a personal IMDB.com userscript"
  21. //input your email and follow the instructions given.
  22. //insert the key you receive inbetween the quotations below (ex. var API_KEY = "ab123c4d";)
  23.  
  24. var API_KEY = "";
  25.  
  26. //- You can ignore everything below here -
  27.  
  28. var cssTransitionTime = 750;
  29. var OMDB_ERROR = false;
  30.  
  31. GM_addStyle(`.copybutton:active,.revealoriginalnamesbutton:active{background-color:#ccc3ac}.copybutton{font-size:13px;padding:0;margin:0 0 0 2px;width:50px;height:18px;position:relative;overflow:hidden}.copybutton::before{content:"Copy";position:absolute;left:7px}.copybutton::after{content:"Copied";position:absolute;left:2px;top:15px;pointer-events:none}.copybutton::after,.copybutton::before{transition:all ${cssTransitionTime*.35}ms}.copybutton.clicked::after{top:-1px}.copybutton.unclicked::after{top:18px}.copybutton.clicked::before{top:-18px}.copybutton.unclicked::before{top:-1px}.copybutton:focus{outline:0}#yearbutton::before{content:"+Year";left:4px}#idbutton::before{content:"ID";left:15px}.itemprop .copybutton{margin-left:5px}.revealoriginalnamesbutton{font-size:13px;padding:0;margin:0 0 0 5px;height:18px}.revealoriginalnamesbutton::before{content:"Reveal Original Titles"}.countwrapper{font-size:16px;padding-top:10px}`);
  32.  
  33. (function main(){
  34. addTitleButtons();
  35. addOtherButtons();
  36. if(window.location.href.match("/name/")) {
  37. relocateAltNames();
  38. }
  39. })();
  40.  
  41. function revealOriginalName(element, id){
  42. GM_xmlhttpRequest({
  43. method: "GET",
  44. url: `https://www.omdbapi.com/?i=${id}&apikey=${API_KEY}`,
  45. onload: function(res) {
  46. var response = JSON.parse(res.responseText);
  47.  
  48. if(response.Response != "False"){
  49. originalTitle = response.Title;
  50. if(originalTitle != undefined){
  51. var span = document.createElement("span");
  52. span.setAttribute("class", "originaltitle");
  53. var text = document.createTextNode(" - " + originalTitle);
  54. span.appendChild(text)
  55.  
  56. if(element.textContent != originalTitle && !element.parentNode.querySelector(".originaltitle")){
  57. element.parentNode.appendChild(span);
  58. updateCounter();
  59. }
  60. }
  61. } else {
  62. if(!OMDB_ERROR && response.Error != "Error getting data."){
  63. OMDB_ERROR = true;
  64. alert(response.Error);
  65. } else {
  66. console.log(response.Error, "Movie ID: " + id, element);
  67. }
  68. }
  69. },
  70. onerror: function(err, a, b){
  71. alert("Error in making the request: ", err, a, b);
  72. }
  73. });
  74. }
  75.  
  76. function updateCounter(){
  77. var parent = document.querySelector(".revealoriginalnamesbutton").parentNode;
  78.  
  79. //init the html if not created already
  80. if(parent.querySelector(".count") == null){
  81. var wrapper = document.createElement("span");
  82. wrapper.setAttribute("class", "countwrapper");
  83. var text = document.createTextNode("Matches found: ");
  84. wrapper.appendChild(text);
  85. var span = document.createElement("span");
  86. span.setAttribute("class", "count");
  87. span.appendChild(document.createTextNode("0"));
  88. wrapper.appendChild(span);
  89. parent.appendChild(wrapper);
  90. }
  91.  
  92. var count = parseInt(parent.querySelector(".count").innerHTML);
  93. parent.querySelector(".count").innerHTML = count + 1;
  94. }
  95.  
  96. function addTitleButtons(){
  97. var title = document.querySelector("h3[itemprop='name'], h1 [class='itemprop']");
  98.  
  99. var titleButton = createButton(title.childNodes[0].textContent.trim());
  100. title.appendChild(titleButton);
  101.  
  102. if(window.location.href.match("/title/")){
  103. var yearButton = createButton(title.textContent.replace(/ +\n +/, " ").trim());
  104. yearButton.setAttribute("id", "yearbutton");
  105. title.appendChild(yearButton);
  106.  
  107. var altTitle = title.nextSibling;
  108. if(altTitle.textContent.trim()){
  109. var altButton = createButton(altTitle.textContent.replace(/ +\n +/, " ").trim());
  110. altTitle.parentNode.insertBefore(altButton, altTitle.nextSibling.nextSibling);
  111. }
  112. }
  113.  
  114. var IDButton = createButton(window.location.pathname.split('/')[2]);
  115. IDButton.setAttribute("id", "idbutton");
  116. title.appendChild(IDButton);
  117. }
  118.  
  119. function addOtherButtons(){
  120. var toAppend = document.querySelectorAll(".itemprop a, .crew_list a, .writers_list a, .filmo-row b");
  121.  
  122. for(var i = 0; i < toAppend.length; i++){
  123. var copyButton = createButton(toAppend[i].textContent.trim());
  124.  
  125. if(window.location.href.match("/title/")){
  126. var td = document.createElement("td");
  127. td.appendChild(copyButton);
  128.  
  129. toAppend[i].parentElement.parentElement.appendChild(td);
  130. } else if(window.location.href.match("/name/")) {
  131. toAppend[i].parentElement.querySelector(".year_column").appendChild(copyButton);
  132. }
  133. }
  134.  
  135.  
  136. //reveal original name button + event listener
  137. //h2 containing "Filmography" doesn't have an id, this is a workaround to get that element
  138. var h2ToAppend = document.evaluate("//h2[contains(., 'Filmography')]", document, null, XPathResult.ANY_TYPE, null).iterateNext();
  139.  
  140. if(h2ToAppend){
  141. var button = document.createElement('button');
  142. button.setAttribute("class", "revealoriginalnamesbutton linkasbutton-secondary");
  143. h2ToAppend.appendChild(button);
  144.  
  145. button.addEventListener("click", function(){
  146. var elements = document.querySelectorAll(".filmo-category-section b a");
  147. var ids = [];
  148.  
  149. for(var i = 0; i < elements.length; i++){
  150. var href = elements[i].href.split("/");
  151. ids[i] = href[4];
  152. }
  153.  
  154. for(var i = 0; i < ids.length; i++){
  155. var error = revealOriginalName(elements[i], ids[i]);
  156. }
  157. });
  158. }
  159. }
  160.  
  161. function relocateAltNames(){
  162. var altNames = document.querySelector("#details-akas");
  163. if(altNames){
  164. var names = altNames.textContent.replace(/\n|Alternate Names:\W +/gm, "").trim().split(" | ").join(", ");
  165.  
  166. var h4 = document.createElement("h4");
  167. h4.setAttribute("class", "inline");
  168. var text = document.createTextNode("Alternative names:");
  169. h4.appendChild(text);
  170.  
  171. var altNamesDiv = document.createElement("div");
  172. altNamesDiv.setAttribute("class", "alt-names txt-block");
  173. altNamesDiv.appendChild(h4)
  174. text = document.createTextNode(names);
  175. altNamesDiv.appendChild(text);
  176.  
  177. var copyButton = createButton(names);
  178. altNamesDiv.appendChild(copyButton);
  179.  
  180. document.querySelector("#overview-top").appendChild(altNamesDiv);
  181. }
  182. }
  183.  
  184. function createButton(copytext){
  185. var button = document.createElement('button');
  186. button.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
  187. button.setAttribute("data-copytext", copytext);
  188.  
  189. button.addEventListener("click", function(e){
  190. GM_setClipboard(e.target.getAttribute("data-copytext"));
  191. e.target.setAttribute("class", "copybutton linkasbutton-secondary clicked");
  192.  
  193. setTimeout(function(){
  194. e.target.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
  195. }, cssTransitionTime);
  196. });
  197.  
  198. return button;
  199. }