nico.ms用bookmarklet

Tweenが対応したらあっさりと使わなくなるだろうな、と思いつつ。

javascript:(function(){var d=document;d.body.appendChild(d.createElement('script')).src='http://j.mp/4C3Nov';copy('http://nico.ms/'+(location.href.match(/^http:\/\/www\.nicovideo\.jp\/watch\/([^\?]*)/)||[0,"\0"])[1]);})();

動画ページで実行すると、nico.ms化した動画URLがクリップボードにコピーされます。


クリップボードにコピー - 素人がプログラミングを勉強していたブログのcopy関数を使わせてもらっています。
http://j.mp/4C3Nov = http://cobodo.hp.infoseek.co.jp/cp.js で、内容はリンク先と全く同じなので、気にする人は自分の管理下に置き直すが吉。

追記

あまりにも貧弱だと思ったのでAPIを使うように変更。
しかしこういう形式のAPIをJSのみで扱うことは、現状、できるのだろうか。
クロスドメインXmlHttpRequestが必要な気がするので、一応JSONP化するラッパ(php)をかます形に。
そして広告なしでphpを動かす公開環境を持ってないのでlocalhostに……w
PHPである必要はないので、GoogleAppEngineとか使えばいいんだろうけど、よくわからんのでとりあえずナシということで。

javascript:(function(){var d=document;d.body.appendChild(d.createElement('script')).src='http://localhost/nicoms.js';})();
  • http://localhost/nicoms.js
function cb(text) {
    var input, success;
    if ("console" in window && "notifyFirebug" in console) {
        console.notifyFirebug([text], "copy", "firebugExecuteCommand");
        success = true;
    } else {
        input = document.createElement("input");
        input.style.position = "absolute";
        input.style.top = "-100px";
        input.value = text;
        input.hidden = true;
        document.body.appendChild(input);
        input.select();
        try {
            success = document.execCommand("copy", false, null);
        } catch (ex) {
            success = false;
        } finally {
            document.body.removeChild(input);
        }
    }
    if (!success) {
        prompt("Press Ctrl+V", text);
    }
    else {
        alert("'" + text + "' copied!");
    }
}

document.body.appendChild(document.createElement('script')).src='http://localhost/nicoms.php?q='+location.href;
  • http://localhost/nicoms.php
cb('<?php echo(htmlspecialchars(file_get_contents('http://nico.ms/q/'.$_GET['q'])),ENT_QUOTES); ?>');

追記2

APIが変なものを返してきても(ある程度)大丈夫なように、ラッパでhtmlspecialcharsをかました。
あとYQLっての使うとJSONP化できるらしい。あとでチェック。