IMDB Copy Buttons V2

Buttons

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

  1. // ==UserScript==
  2. // @name IMDB Copy Buttons V2
  3. // @namespace http://kmcgurty.com/
  4. // @version 1.4.1
  5. // @description Buttons
  6. // @author Kmcgurty
  7. // @match https://www.imdb.com/title/*
  8. // @match https://www.imdb.com/name/*
  9. // @grant GM_addStyle
  10. // @grant GM_setClipboard
  11. // ==/UserScript==
  12.  
  13. var css = {};
  14. css.transitiontime = 750;
  15. css.directorleft = "2";
  16.  
  17. //not sure why firefox and chrome display this button differently
  18. var isFirefox = typeof InstallTrigger !== 'undefined';
  19. if(isFirefox){css.directorleft = "-1"};
  20.  
  21.  
  22. GM_addStyle(`.copybutton:active,.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:3px;top:15px;font-size:12px;pointer-events:none}.copybutton::after,.copybutton::before{transition:top ${css.transitiontime*.35}ms}.copybutton.clicked::after{top:0}.copybutton.unclicked::after{top:18px}.copybutton.clicked::before{top:-18px !important}.copybutton.unclicked::before{top:-1px}.copybutton:focus{outline:0}#yearbutton::before{content:"+Year";left:4px}#idbutton::before{content:"ID";left:15px}#directorbutton::before{content:"+Director";left:${css.directorleft}px;top:2px;font-size:9px}.itemprop .copybutton{margin-left:5px}`);
  23.  
  24. (function main(){
  25. addButtons();
  26.  
  27. if(window.location.href.match("/name/")) {
  28. relocateAltNames();
  29. }
  30. })();
  31.  
  32. function addButtons(){
  33. var titleNode = document.querySelector("h3[itemprop='name'], #overview-top h1");
  34.  
  35. var br = document.createElement("br");
  36. titleNode.appendChild(br);
  37.  
  38. //title button
  39. var copyText = titleNode.childNodes[0].textContent.trim();
  40. var button = createButton(copyText, "titlebutton");
  41. titleNode.appendChild(button);
  42.  
  43. if(window.location.href.match("/title/")){
  44. //+year button
  45. copyText = titleNode.textContent.replace(/ +\n +/, " ").trim();
  46. button = createButton(copyText, "yearbutton");
  47. titleNode.appendChild(button);
  48.  
  49.  
  50. //+director button
  51. var directorName = document.querySelector(".titlereference-overview-section li").textContent.trim();
  52. var title = titleNode.childNodes[0].textContent.trim();
  53. copyText = title + " - " + directorName;
  54. button = createButton(copyText, "directorbutton");
  55. titleNode.appendChild(button);
  56.  
  57.  
  58. //alt title button
  59. var altTitleNode = titleNode.nextSibling;
  60. if(altTitleNode.textContent.trim()){
  61. copyText = altTitleNode.textContent.replace(/ +\n +/, " ").trim();
  62. button = createButton(copyText);
  63. altTitleNode.parentNode.insertBefore(button, altTitleNode.nextSibling.nextSibling);
  64. }
  65.  
  66.  
  67. //director/creator button
  68. var directorNode = document.querySelector(".titlereference-overview-section li")
  69. copyText = titleNode.textContent.trim();
  70. button = createButton(copyText);
  71. directorNode.appendChild(button);
  72. }
  73.  
  74.  
  75. //id button
  76. copyText = window.location.pathname.split('/')[2];
  77. button = createButton(copyText, "idbutton");
  78. titleNode.appendChild(button);
  79.  
  80.  
  81. //actor names and movies, everything below the title
  82.  
  83. var listToAppend = document.querySelectorAll(".itemprop a, .crew_list a, .writers_list a, .filmo-row b");
  84.  
  85. for(var i = 0; i < listToAppend.length; i++){
  86. var copyText = listToAppend[i].textContent.trim();
  87. button = createButton(copyText);
  88.  
  89. if(window.location.href.match("/title/")){
  90. var td = document.createElement("td");
  91. td.appendChild(button);
  92.  
  93. listToAppend[i].parentElement.parentElement.appendChild(td);
  94. } else if(window.location.href.match("/name/")) {
  95. listToAppend[i].parentElement.querySelector(".year_column").appendChild(button);
  96. }
  97. }
  98. }
  99.  
  100. function relocateAltNames(){
  101. var altNames = document.querySelector("#details-akas");
  102. if(altNames){
  103. var names = altNames.textContent.replace(/\n|Alternate Names:\W +/gm, "").trim().split(" | ").join(", ");
  104.  
  105. var h4 = document.createElement("h4");
  106. h4.setAttribute("class", "inline");
  107. var text = document.createTextNode("Alternative names:");
  108. h4.appendChild(text);
  109.  
  110. var altNamesDiv = document.createElement("div");
  111. altNamesDiv.setAttribute("class", "alt-names txt-block");
  112. altNamesDiv.appendChild(h4)
  113. text = document.createTextNode(names);
  114. altNamesDiv.appendChild(text);
  115.  
  116. var copyButton = createButton(names);
  117. altNamesDiv.appendChild(copyButton);
  118.  
  119. document.querySelector("#overview-top").appendChild(altNamesDiv);
  120. }
  121. }
  122.  
  123. function createButton(copytext, elementID){
  124. var elementID = elementID || "";
  125.  
  126. var button = document.createElement('button');
  127. button.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
  128. button.setAttribute("id", elementID);
  129. button.setAttribute("data-copytext", copytext);
  130.  
  131. button.addEventListener("click", function(e){
  132. GM_setClipboard(e.target.getAttribute("data-copytext"));
  133. e.target.setAttribute("class", "copybutton linkasbutton-secondary clicked");
  134.  
  135. setTimeout(function(){
  136. e.target.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
  137. }, css.transitiontime);
  138. });
  139.  
  140. return button;
  141. }