IMDB Copy Buttons V2

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         IMDB Copy Buttons V2
// @namespace    http://kmcgurty.com/
// @version      1.3
// @description  Adds buttons to IMDB that allow you to copy the respective movie titles and actor names
// @author       Kmcgurty
// @match        https://www.imdb.com/title/*
// @match        https://www.imdb.com/name/*
// @grant        GM_addStyle
// @grant        GM_setClipboard
// ==/UserScript==

var cssTransitionTime = 750;

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}`);

(function main(){
    addTitleButtons();
    addOtherButtons();

    if(window.location.href.match("/name/")) {
        relocateAltNames();
    }
})();

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);
        }
    }
}

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;
}