gist fill in fileName automatically

Fill in the filename automatically When pasting a Greasemonkey script.

目前為 2014-05-18 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           gist fill in fileName automatically
// @namespace      http://efcl.info/
// @description    Fill in the filename automatically When pasting a Greasemonkey script.
// @include        http://gist.github.com/
// @include        https://gist.github.com/
// @version 0.0.1.20140518104258
// ==/UserScript==

/*  GRATITUDE
 ペーストされたテキストを置き換えるスクリプト
 http://gist.github.com/338606
 */
var fileExtension = ".user.js";
var addAutoEvent = function(fileID) {
    var editArea = document.getElementsByName("file_contents[gistfile" + fileID + "]")[0];
    var editFileName = document.getElementsByName("file_name[gistfile" + fileID + "]")[0];
    var editDescription = document.getElementsByName("description")[0];
    editArea.addEventListener("paste", function(e) {
        // この瞬間はまだtargetのvalueが空である。
        var obj = e.target;
        obj.addEventListener('input', function(evt) {
            // クリップボードの内容がinputされた
            evt.currentTarget.removeEventListener(evt.type, arguments.callee, false);
            var pasteValue = obj.value;
            // name
            var atName = pasteValue.match(/@name\s+([\w\s]+)/i);
            atName = atName && atName[1];
            // description
            var atDesc = pasteValue.match(/@description\s+(.*)/i);
            atDesc = atDesc && atDesc[1];
            if (atName && !editFileName.value) {
                atName = atName.replace(/\s+$/, "");
                // m = m.replace(" " , "_" , "g");// 第3引数 繰り返し
                editFileName.value = atName + fileExtension;
                // console.log(m + fileExtension);
                if(atDesc){
                    editDescription.value = atDesc;
                }
                editFileName.focus();
            }
        }, false);
    }, false);
};
// 追加されたテキストエリアにもイベントをつける
var addButton = document.getElementById("add-gist");
addButton.addEventListener("click", function() {
    document.getElementById("files").addEventListener("DOMNodeInserted", function(e) {
        // テキストエリアが追加された         
        e.currentTarget.removeEventListener(e.type, arguments.callee, false);
        var editAreaLength = document.getElementsByClassName("file").length;
        addAutoEvent(editAreaLength);
    }, false);
}, false);
// 最初から表示されているテキストエリアにイベントをつける
addAutoEvent(1);