执行js脚本

当按下快捷键Ctrl+P时,弹出一个输入框,输入JavaScript或jQuery脚本,点击确定后执行脚本,可用于重复输入,脚本执行工作

目前為 2023-09-06 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         执行js脚本
// @namespace    http://tampermonkey.net/
// @icon         https://img-blog.csdnimg.cn/20181221195058594.gif
// @version      1.0
// @description  当按下快捷键Ctrl+P时,弹出一个输入框,输入JavaScript或jQuery脚本,点击确定后执行脚本,可用于重复输入,脚本执行工作
// @author       wll
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @require      https://greasyfork.org/scripts/471299-toastify-js/code/toastifyjs.js?version=1222923
// @resource     css2 https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_getResourceText
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('keydown', function(event) {
        // 检查是否按下Ctrl和P键
        if (event.ctrlKey && event.key === 'p') {
            event.preventDefault();

            // 提示输入脚本
            var script = prompt('请输入JavaScript或jQuery脚本:');
            if (script) {
                // 检查脚本语法
                try {
                    // 使用eval执行脚本
                    eval(script);
                } catch (error) {
                    // 提示语法错误
                    alert('脚本语法错误:' + error.message);
                    showtoastMessage('脚本语法错误:' + error.message);
                }
            }
        }
    });

    /**
     * 在页面右下角显示
     * @param msgText
     */
    function showtoastMessage(msgText){
        GM_addStyle(GM_getResourceText("css2"));
        Toastify({
            text: msgText,
            duration: 1500,
            newWindow: false,
            gravity: "bottom", // `top` or `bottom`
            position: "right", // `left`, `center` or `right`
            style: {
                background: "linear-gradient(to right, #00b09b, #96c93d)",
            }
        }).showToast();
    }
})();