IMDB Copy Buttons V2

Buttons

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