// ==UserScript==
// @name         auto-copy
// @namespace    http://tampermonkey.net/
// @version      0.0
// @description  auto-answer
// @author       Priate
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addStyle
// @connect     eemm.me
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_addStyle
// @grant       GM_deleteValue
// @grant       GM_xmlhttpRequest
// @grant       GM_setClipboard
// @grant       GM_registerMenuCommand
// @run-at      document-start
// @require      https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js
// @require      https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js
// @icon         https://priate.oss-cn-beijing.aliyuncs.com/products/picture/icon/q-note.png
// @require      https://cdn.jsdelivr.net/npm/sweetalert@2.1.2/dist/sweetalert.min.js
// @supportURL   https://greasyfork.org/zh-CN/scripts/421876/feedback
// @source       https://github.com/PriateXYF
// @license      MIT
// ==/UserScript==
(function () {
    'use strict';
    const isDebug = true

    // GM_setValue('auto_answer_setting')

    function init() {
        const auto_answer_div = document.createElement("div");
        auto_answer_div.innerHTML = `
<div id="auto_answer_div">
<div v-if="show">
{{text}}
</div>
</div>
            `
        document.querySelector("html").appendChild(auto_answer_div)
        // if (GM_getValue('auto_answer_setting') == null) {
        //     GM_setValue('auto_answer_setting',
        //         {
        //             show: false,
        //         }
        //     )
        // }
    }

    init()


    function SelectText() {
        let selecter = ""
        try {
            selecter = window.getSelection().toString();
            if (selecter != null && selecter.trim() != "") {
                return selecter;
            }
        } catch (err) {
            selecter = document.selection.createRange();
            const s = selecter.text;
            if (s != null && s.trim() != "") {
                return selecter;
            }
        }
        console.log("选中失败了")
        return selecter;
    }

    const vm = new Vue({
        el: '#auto_answer_div',
        data() {
            return {
                enable: true,
                show: false,
                text: '',
            }
        },
        methods: {},
        mounted() {
            const _this = this;
            document.onkeydown = function (event) {
                /**
                 * ctrlKey+Q 开启/关闭
                 * shiftKey+Q && enable 展示、隐藏
                 * shiftKey+A && enable 获取当前dom的所有文字
                 * shiftKey+S && enable 复制选中的文字
                 * shiftKey+C && enable 将text复制到剪切板
                 */
                const keyCode = event.keyCode || event.which || event.charCode;
                const ctrlKey = event.ctrlKey;
                const shiftKey = event.shiftKey;
                const altKey = event.altKey;
                if (ctrlKey && keyCode === 81) {
                    if (isDebug) {
                        console.log("ctrlKey+Q 开启/关闭", _this.enable);
                    }
                    _this.enable = !_this.enable
                } else if (shiftKey && keyCode === 81) {
                    if (!_this.enable) {
                        _this.text = 'ctrlKey+Q 开启'
                    }
                    _this.show = !_this.show
                    if (isDebug) {
                        console.log("shiftKey+Q 展示:", _this.enable);
                    }
                } else if (shiftKey && keyCode === 65) {
                    if (!_this.enable) {
                        _this.text = 'ctrlKey+Q 开启'
                        _this.show = true;
                        return
                    }
                    _this.text = document.body.innerText
                    if (isDebug) {
                        console.log("shiftKey+A 获取body.text:", _this.text);
                    }
                } else if (shiftKey && keyCode === 67) {
                    if (!_this.enable) {
                        _this.text = 'ctrlKey+Q 开启'
                        _this.show = true;
                        return
                    }
                    GM.setClipboard(_this.text);
                    if (isDebug) {
                        console.log("shiftKey+C 复制到剪切板:", _this.text);
                    }
                } else if (shiftKey && keyCode === 83) {
                    if (!_this.enable) {
                        _this.text = 'ctrlKey+Q 开启'
                        _this.show = true;
                        return
                    }
                    const selectText = SelectText();
                    GM.setClipboard(selectText);
                    if (isDebug) {
                        console.log("shiftKey+S 复制选中的文字:", selectText);
                    }
                }
            }
        }
    });
})();