NickGPT

Use NickGPT on Google Search page!

目前為 2023-04-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name         NickGPT
// @namespace    https://ugpt.nickname4th.vip/
// @version      0.7
// @description  Use NickGPT on Google Search page!
// @author       InJeCTrL
// @match        *://www.google.com/search*
// @match        *://www.google.com.hk/search*
// @match        *://www.google.co.uk/search*
// @match        *://www.baidu.com*
// @match        *://www.baidu.com/s*
// @match        *://so.toutiao.com/search*
// @match        *://cn.bing.com/search*
// @match        *://www.bing.com/search*
// @match        *://duckduckgo.com/?*
// @match        *://www.so.com/s?*
// @match        *://www.sogou.com/*
// @grant        window.onurlchange
// @grant        GM_addElement
// @icon         https://s1.ax1x.com/2023/04/21/p9E40Ve.png
// @require      https://code.jquery.com/jquery-3.6.0.js
// @require      https://cdn.bootcdn.net/ajax/libs/jqueryui/1.13.2/jquery-ui.min.js
// @resource css https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    function getSearchContent() {
        var url = window.location.href;
        if (url.indexOf("www.baidu.com") != -1) {
            return document.getElementById('kw').value;
        } else if (url.indexOf("www.google.") != -1) {
            return document.getElementsByTagName('textarea')[0].textContent;
        } else if (url.indexOf("so.toutiao.com") != -1) {
            return document.getElementsByTagName('input')[0].value;
        } else if (url.indexOf("bing.com") != -1) {
            return document.getElementById('sb_form_q').value;
        } else if (url.indexOf("duckduckgo.com") != -1) {
            return document.getElementById('search_form_input').value;
        } else if (url.indexOf("www.so.com") != -1) {
            return document.getElementById('keyword').value;
        } else if (url.indexOf("www.sogou.com") != -1) {
            return document.getElementById('upquery').value;
        }
    }

    function searchKeyword() {
        document.getElementById('nickgpt-wnd')
            .contentWindow.postMessage({
            "nickGPT": getSearchContent()
        }, '*');
    }

    function nickgptHandler(message) {
        if (message.data == "chatready") {
            searchKeyword();
        }
    }

    function insertWnd() {
        var box = document.createElement("div");
        var ifr = document.createElement("iframe");
        ifr.id = "nickgpt-wnd";
        ifr.src = "https://ugpt.nickname4th.vip";
        box.id = "nickgpt-box";
        document.body.insertBefore(box, document.body.firstChild);
        GM_addElement(box, 'iframe', {
            src: "https://ugpt.nickname4th.vip",
            id: "nickgpt-wnd"
        });

        $(function () {
            $("#nickgpt-box").draggable();
        });

        window.addEventListener("message", nickgptHandler);
    }

    function insertStyle() {
        var css = "\
#nickgpt-box {\
border: 1px solid #cceff5;\
position: fixed;\
right: 10%;\
top: 8%;\
width: 30%;\
height: 80%;\
z-index: 998;\
padding-top:25px;\
background: #1E90FF;\
cursor: grab;\
border-radius: 25px;\
box-shadow: 1px 1px 1px 1px grey;\
}\
#nickgpt-wnd {\
border: none;\
width: 100%;\
height: 100%;\
z-index: 999;\
background: white;\
border-radius: 25px;\
}";
        const style = document.createElement('style');
        style.type = 'text/css';
        style.appendChild(document.createTextNode(css));
        document.children[0].appendChild(style);
    }

    if (window.onurlchange === null &&
        (window.location.href.indexOf("www.so.com") != -1 || window.location.href.indexOf("www.baidu.com") != -1)) {
        window.addEventListener('urlchange', (info) => {
            searchKeyword();
        });
    }

    insertStyle();
    insertWnd();
})();