使用Tampermonkey复制Stack Overflow代码。

问题描述 投票:0回答:1

使用TamperMonkey快速可靠地复制Stack Overflow代码的优化方法是什么?

javascript greasemonkey tampermonkey userscripts
1个回答
-1
投票

这里有一个功能脚本,可以让复制Stack Overflow代码片段的速度更快。这个脚本可以作为Stack Overflow网站的附加功能。这个脚本将使像我这样的程序员的事情变得更容易。

// ==UserScript==
// @name         Stack Code
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Copy code snippets inside Stack Overflow website!
// @author       Youssof H.
// @match        https://*.stackoverflow.com/*
// @match        https://*.stackexchange.com/*
// @match        https://*.superuser.com/*
// @grant        none
// ==/UserScript==

var $ = window.jQuery; // basically to remove IDE warnings

var copyText; //Variable that holds code value
$(document).ready(function(){
    $("pre").before("<button class='copy_btn'>copy</button><br>"); //Add a button before every code snippet
    $(".copy_btn").click(function(){
        $(this).append('<textarea id="copytxt" style="font-size: 0px;contenteditable:false;background-color: rgb(255, 255, 255);" placeholder="Code to copy..."></textarea>'); //Make a textarea that will hold code for small instants
        copyText = $("#copytxt");
        var sel_code = $(this).nextAll("pre:eq(0)").find("code").text(); //Selects code
        copyText.val(sel_code); //Sets the value of the texarea
        copyCode(); //Run copying function
    });
});

function copyCode() {
    copyText.select(); //Select code in texarea
    document.execCommand("copy"); //Execute command copy text from selected input(textarea)
    copyText.remove(); //Delete textarea when finish
}

将Tampermonkey扩展添加到你的浏览器中,然后复制并粘贴该代码到一个新的用户脚本中。完成后访问这个页面来测试它的功能。在每个代码片段前都会出现一个蓝色的按钮,一旦你点击该按钮,它将复制以下内容。

注意:当你访问网站时,必须刷新你的网站。 当你对你的脚本进行增强时,你必须刷新你访问的网站!

希望对大家有所帮助。接受任何的增强。

© www.soinside.com 2019 - 2024. All rights reserved.