// ==UserScript==
// @name IMDB Copy Buttons V2
// @namespace http://kmcgurty.com/
// @version 1.2.1
// @description Buttons
// @author Kmcgurty
// @match https://www.imdb.com/title/*
// @match https://www.imdb.com/name/*
// @connect omdbapi.com
// @grant GM_addStyle
// @grant GM_setClipboard
// @grant GM_xmlhttpRequest
// ==/UserScript==
//~README~
//
//to use the "Reveal original titles" feature, you need to get an API key from OMDB.
//you can get that from here: http://www.omdbapi.com/apikey.aspx
//select "FREE! (1,000 daily limit)"
//use is "For a personal IMDB.com userscript"
//input your email and follow the instructions given.
//insert the key you receive inbetween the quotations below (ex. var API_KEY = "ab123c4d";)
var API_KEY = "";
//- You can ignore everything below here -
var cssTransitionTime = 750;
var OMDB_ERROR = false;
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}`);
(function main(){
addTitleButtons();
addOtherButtons();
if(window.location.href.match("/name/")) {
relocateAltNames();
}
})();
function revealOriginalName(element, id){
GM_xmlhttpRequest({
method: "GET",
url: `https://www.omdbapi.com/?i=${id}&apikey=${API_KEY}`,
onload: function(res) {
var response = JSON.parse(res.responseText);
if(response.Response != "False"){
var originalTitle = response.Title;
if(originalTitle != undefined){
var span = document.createElement("span");
span.setAttribute("class", "originaltitle");
var text = document.createTextNode(" - " + originalTitle);
span.appendChild(text)
if(element.textContent != originalTitle && !element.parentNode.querySelector(".originaltitle")){
element.parentNode.appendChild(span);
updateCounter();
}
}
} else {
if(!OMDB_ERROR && response.Error != "Error getting data."){
OMDB_ERROR = true;
alert(response.Error);
} else {
console.log(response.Error, "Movie ID: " + id, element);
}
}
},
onerror: function(err, a, b){
alert("Error in making the request: ", err, a, b);
}
});
}
function updateCounter(){
var parent = document.querySelector(".revealoriginalnamesbutton").parentNode;
//init the html if not created already
if(parent.querySelector(".count") == null){
var wrapper = document.createElement("span");
wrapper.setAttribute("class", "countwrapper");
var text = document.createTextNode("Matches found: ");
wrapper.appendChild(text);
var span = document.createElement("span");
span.setAttribute("class", "count");
span.appendChild(document.createTextNode("0"));
wrapper.appendChild(span);
parent.appendChild(wrapper);
}
var count = parseInt(parent.querySelector(".count").innerHTML);
parent.querySelector(".count").innerHTML = count + 1;
}
function addTitleButtons(){
var title = document.querySelector("h3[itemprop='name'], h1 [class='itemprop']");
var titleButton = createButton(title.childNodes[0].textContent.trim());
title.appendChild(titleButton);
if(window.location.href.match("/title/")){
var yearButton = createButton(title.textContent.replace(/ +\n +/, " ").trim());
yearButton.setAttribute("id", "yearbutton");
title.appendChild(yearButton);
var altTitle = title.nextSibling;
if(altTitle.textContent.trim()){
var altButton = createButton(altTitle.textContent.replace(/ +\n +/, " ").trim());
altTitle.parentNode.insertBefore(altButton, altTitle.nextSibling.nextSibling);
}
}
var IDButton = createButton(window.location.pathname.split('/')[2]);
IDButton.setAttribute("id", "idbutton");
title.appendChild(IDButton);
}
function addOtherButtons(){
var toAppend = document.querySelectorAll(".itemprop a, .crew_list a, .writers_list a, .filmo-row b");
for(var i = 0; i < toAppend.length; i++){
var copyButton = createButton(toAppend[i].textContent.trim());
if(window.location.href.match("/title/")){
var td = document.createElement("td");
td.appendChild(copyButton);
toAppend[i].parentElement.parentElement.appendChild(td);
} else if(window.location.href.match("/name/")) {
toAppend[i].parentElement.querySelector(".year_column").appendChild(copyButton);
}
}
//reveal original name button + event listener
//h2 containing "Filmography" doesn't have an id, this is a workaround to get that element
var h2ToAppend = document.evaluate("//h2[contains(., 'Filmography')]", document, null, XPathResult.ANY_TYPE, null).iterateNext();
if(h2ToAppend){
var button = document.createElement('button');
button.setAttribute("class", "revealoriginalnamesbutton linkasbutton-secondary");
h2ToAppend.appendChild(button);
button.addEventListener("click", function(){
var elements = document.querySelectorAll(".filmo-category-section b a");
var ids = [];
for(var i = 0; i < elements.length; i++){
var href = elements[i].href.split("/");
ids[i] = href[4];
}
for(i = 0; i < ids.length; i++){
var error = revealOriginalName(elements[i], ids[i]);
}
});
}
}
function relocateAltNames(){
var altNames = document.querySelector("#details-akas");
if(altNames){
var names = altNames.textContent.replace(/\n|Alternate Names:\W +/gm, "").trim().split(" | ").join(", ");
var h4 = document.createElement("h4");
h4.setAttribute("class", "inline");
var text = document.createTextNode("Alternative names:");
h4.appendChild(text);
var altNamesDiv = document.createElement("div");
altNamesDiv.setAttribute("class", "alt-names txt-block");
altNamesDiv.appendChild(h4)
text = document.createTextNode(names);
altNamesDiv.appendChild(text);
var copyButton = createButton(names);
altNamesDiv.appendChild(copyButton);
document.querySelector("#overview-top").appendChild(altNamesDiv);
}
}
function createButton(copytext){
var button = document.createElement('button');
button.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
button.setAttribute("data-copytext", copytext);
button.addEventListener("click", function(e){
GM_setClipboard(e.target.getAttribute("data-copytext"));
e.target.setAttribute("class", "copybutton linkasbutton-secondary clicked");
setTimeout(function(){
e.target.setAttribute("class", "copybutton linkasbutton-secondary unclicked");
}, cssTransitionTime);
});
return button;
}