wechat add copy title&url button

try to take over the world!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         wechat add copy title&url button
// @namespace    http://tampermonkey.net/
// @version      0.02
// @description  try to take over the world!
// @author       You
// @include      https://mp.weixin.qq.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var Start={};
    Start.one_create_btn = function(){
        var Div = document.getElementById("js_article");
        var button = document.createElement("input");
        button.setAttribute("type", "button");
        button.setAttribute("value", "copy_title");
        button.setAttribute("id", "copy_btn1");
        button.setAttribute("class", "class_copy_btn1");

        button.style.width = "50%";
        button.style.height = "50px";
        button.style.marginLeft = "auto"
        button.style.marginRight = "auto"

        document.body.insertBefore(button, Div);

        document.getElementById("copy_btn1").onclick=function(){
            // IE浏览器
            //var clipBoardContent=document.getElementById("activity-name").innerText;
            //var clipBoardContent=document.title;
            var clipBoardContent=document.title;
            if (window.clipboardData) {
                window.clipboardData.clearData();
                window.clipboardData.setData("Text", clipBoardContent);
                alert("复制成功!");
            }else{
                if (!document.queryCommandSupported('copy')) {
                    throw new Error('document.execCommand method not support copy command');
                }
                const input = document.createElement('input'); // 要input类型
                //input.style.cssText = 'display: block;opacity: 0;position: absolute;left: -10000px;top: -10000px;z-index: -1;width: 1px;height: 1px;'
                document.body.appendChild(input);
                input.value = clipBoardContent;
                input.select();
                document.execCommand('copy');
                document.body.removeChild(input);
            }



        }

        // Error 不能通过这种属性和值的方式建立关联
        /*
        button.setAttribute("onclick", "copyUrl()");
        function copyUrl()
        {
            alert("复制成功!");
        }
        */

    };
    Start.two_create_btn = function(){
        var Div = document.getElementById("js_article");
        var button = document.createElement("input");
        button.setAttribute("type", "button");
        button.setAttribute("value", "copy_url");
        button.setAttribute("id", "copy_btn2");
        button.setAttribute("class", "class_copy_btn1");

        button.style.width = "50%";
        button.style.height = "50px";
        button.style.marginLeft = "auto"
        button.style.marginRight = "auto"

        document.body.insertBefore(button, Div);

        document.getElementById("copy_btn2").onclick=function(){
            // IE浏览器
            //var clipBoardContent=document.getElementById("activity-name").innerText;
            //var clipBoardContent=document.title;
            var clipBoardContent=document.querySelector("head > meta:nth-child(14)").content;
            if (window.clipboardData) {
                window.clipboardData.clearData();
                window.clipboardData.setData("Text", clipBoardContent);
                alert("复制成功!");
            }else{
                if (!document.queryCommandSupported('copy')) {
                    throw new Error('document.execCommand method not support copy command');
                }
                const input = document.createElement('input'); // 要input类型
                //input.style.cssText = 'display: block;opacity: 0;position: absolute;left: -10000px;top: -10000px;z-index: -1;width: 1px;height: 1px;'
                document.body.appendChild(input);
                input.value = clipBoardContent;
                input.select();
                document.execCommand('copy');
                document.body.removeChild(input);
            }



        }
    }
    Start.one_create_btn();
    Start.two_create_btn();



})();