IMDB Copy Buttons V2

Adds buttons to IMDB that allow you to copy the respective movie titles and actor names

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

  1. // ==UserScript==
  2. // @name IMDB Copy Buttons V2
  3. // @namespace http://kmcgurty.com/
  4. // @version 1.3
  5. // @description Adds buttons to IMDB that allow you to copy the respective movie titles and actor names
  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 cssTransitionTime = 750;
  14.  
  15. 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: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}`);
  16.  
  17. (function main(){
  18. addTitleButtons();
  19. addOtherButtons();
  20.  
  21. if(window.location.href.match("/name/")) {
  22. relocateAltNames();
  23. }
  24. })();
  25.  
  26. function addTitleButtons(){
  27. var title = document.querySelector("h3[itemprop='name'], h1 [class='itemprop']");
  28.  
  29. var titleButton = createButton(title.childNodes[0].textContent.trim());
  30. title.appendChild(titleButton);
  31.  
  32. if(window.location.href.match("/title/")){
  33. var yearButton = createButton(title.textContent.replace(/ +\n +/, " ").trim());
  34. yearButton.setAttribute("id", "yearbutton");
  35. title.appendChild(yearButton);
  36.  
  37. var altTitle = title.nextSibling;
  38. if(altTitle.textContent.trim()){
  39. var altButton = createButton(altTitle.textContent.replace(/ +\n +/, " ").trim());
  40. altTitle.parentNode.insertBefore(altButton, altTitle.nextSibling.nextSibling);
  41. }
  42. }
  43.  
  44. var IDButton = createButton(window.location.pathname.split('/')[2]);
  45. IDButton.setAttribute("id", "idbutton");
  46. title.appendChild(IDButton);
  47. }
  48.  
  49. function addOtherButtons(){
  50. var toAppend = document.querySelectorAll(".itemprop a, .crew_list a, .writers_list a, .filmo-row b");
  51.  
  52. for(var i = 0; i < toAppend.length; i++){
  53. var copyButton = createButton(toAppend[i].textContent.trim());
  54.  
  55. if(window.location.href.match("/title/")){
  56. var td = document.createElement("td");
  57. td.appendChild(copyButton);
  58.  
  59. toAppend[i].parentElement.parentElement.appendChild(td);
  60. } else if(window.location.href.match("/name/")) {
  61. toAppend[i].parentElement.querySelector(".year_column").appendChild(copyButton);
  62. }
  63. }
  64. }
  65.  
  66. function relocateAltNames(){
  67. var altNames = document.querySelector("#details-akas");
  68. if(altNames){
  69. var names = altNames.textContent.replace(/\n|Alternate Names:\W +/gm, "").trim().split(" | ").join(", ");
  70.  
  71. var h4 = document.createElement("h4");
  72. h4.setAttribute("class", "inline");
  73. var text = document.createTextNode("Alternative names:");
  74. h4.appendChild(text);
  75.  
  76. var altNamesDiv = document.createElement("div");
  77. altNamesDiv.setAttribute("class", "alt-names txt-block");
  78. altNamesDiv.appendChild(h4)
  79. text = document.createTextNode(names);
  80. altNamesDiv.appendChild(text);
  81.  
  82. var copyButton = createButton(names);
  83. altNamesDiv.appendChild(copyButton);
  84.  
  85. document.querySelector("#overview-top").appendChild(altNamesDiv);
  86. }
  87. }
  88.  
  89. function createButton(copytext){
  90. var button = document.createElement('button');
  91. button.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
  92. button.setAttribute("data-copytext", copytext);
  93.  
  94. button.addEventListener("click", function(e){
  95. GM_setClipboard(e.target.getAttribute("data-copytext"));
  96. e.target.setAttribute("class", "copybutton linkasbutton-secondary clicked");
  97.  
  98. setTimeout(function(){
  99. e.target.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
  100. }, cssTransitionTime);
  101. });
  102.  
  103. return button;
  104. }