AddTweetButtonToCustomTest

コードテストにツイートボタンを追加します

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        AddTweetButtonToCustomTest
// @namespace   https://greatest.deepsurf.us/ja/scripts/369839-addtweetbuttontocustomtest
// @version     1.0.1
// @description コードテストにツイートボタンを追加します
// @author      keymoon
// @license     MIT
// @homepage    https://greatest.deepsurf.us/ja/scripts/369839-addtweetbuttontocustomtest
// @supportURL  https://twitter.com/kymn_
// @match       https://beta.atcoder.jp/contests/*/custom_test
// @match       https://*.contest.atcoder.jp/custom_test
// ==/UserScript==

(function () {
var outputElem = getOutputElem();
console.log(outputElem)

var tweetStr = trimForTweet(outputElem.text())

var tweetScript =
`<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>`;
var tweetBtn = 
`<a href="https://twitter.com/intent/tweet?text=${tweetStr}" class="btn btn-default col-xs-offset-8 col-xs-4" rel="nofollow" onclick="window.open(encodeURI(decodeURI(this.href)),'twwindow','width=550, height=450, personalbar=0, toolbar=0, scrollbars=1'); return false;">ツイート</a>`;
outputElem.after(tweetScript)
outputElem.after(tweetBtn)

function getOutputElem() {
    return $(isBeta() ? '#stdout' : 'textarea[name="output"]');
    function isBeta() {
        return location.href.split("//")[1].substr(0,4) === "beta";
    }
}

function trimForTweet(str) {
    var tweetStr = "";
    var count = 0;
    Array.prototype.forEach.call(str, 
    function(c) {
        if(isHalf(c)) count += 1;
        else count += 2;
        if (count <= 280) tweetStr += c;
        function isHalf(c){
            if ( (c >= 0x0 && c < 0x81) || (c == 0xf8f0) ||
                    (c >= 0xff61 && c < 0xffa0) || (c >= 0xf8f1 && c < 0xf8f4)) {
                return true;
            }
        }
    });
    return encodeURI(tweetStr);
}
})();