【スクリプト】検索欄のカーソル位置の単語に二重引用符を追加する【公開】

プログラミング

手軽に二重引用符を付加できるようなスクリプトをつくってみました。二重引用符を付加したい単語の位置にカーソルを移動させてF2キーを押すと機能します。完全一致検索を良く使う人は利用してみてください!

なお、一部のサイト(メルカリ)では上手く動かないです。ご了承ください! F2キー以外のキーを割り当てたい場合は、コード内のF2の記述を任意のキーに変更してください。

ちなみに、割りとゴリ押しです。IndexOfAnyがあればスマートにできたから……。っぱC#よ。

// ==UserScript==
// @name         AddDoubleQuotes
// @namespace    https://aldelog.fc2.page/?p=9
// @version      0.1
// @description  Pressing the F2 key adds double quotes to the word at the text cursor position. Pressing Alt + F2 adds double quotes to the entire text.
// @description:ja  F2キーを押すとテキストカーソル位置の単語に対して二重引用符を付加します。Alt + F2キーを押すとテキスト全体に対して二重引用符を付加します。
// @author       Alde
// @match        *://*/*
// @grant        none
// ==/UserScript==

document.addEventListener('keyup', keyup_event);
document.addEventListener('keydown', keydown_event);
var IsRightAfterAddquote = false;

function strIns(str, idx, val){
    var res = str.slice(0, idx) + val + str.slice(idx);
    return res;
};

function keydown_event(e) {

    if (location.host == "twitter.com")
    {
        if (document.activeElement.value != null)
        {
            var SearchWord = document.activeElement.value;
        }
        else
        {return;}

        if(e.key === 'Enter' && IsRightAfterAddquote){

            location.href = ('https://twitter.com/search?q=' + encodeURI(SearchWord));
        }
    }

    IsRightAfterAddquote = false;
};

function keyup_event(e) {

    IsRightAfterAddquote = false;

    if(e.altKey)
    {
        if(e.key === 'F2')
        {
             if (document.activeElement.value != null)
             {
                 document.activeElement.value = "\"" + document.activeElement.value + "\"";
                 IsRightAfterAddquote = true;
             }
        }
    }
    else if(e.key === 'F2'){

        if (document.activeElement.value != null)
        {
            var SearchBox = document.activeElement;
            var SearchWord = SearchBox.value;
            var i = SearchBox.selectionStart;
            var j = SearchBox.selectionEnd;

            if (i != j)
            {
                SearchWord = strIns(SearchWord, i, "\"");
                SearchWord = strIns(SearchWord, j + 1, "\"");
                document.activeElement.value = SearchWord;
                return false;
            }

            var mae = SearchWord.substring(0, i);
            var space1_1 = mae.lastIndexOf(' ');
            var space1_2 = mae.lastIndexOf(' ');
            var space1 = (space1_1 >= space1_2) ? space1_1 : space1_2;
            var maefx;

            if (space1 < 0)
            {
                maefx = mae;
            }
            else
            {
                maefx = mae.substring(space1 + 1, i);
            }
            var ato = SearchWord.substring(i);
            var space2_1 = ato.indexOf(' ');
            var space2_2 = ato.indexOf(' ');

            var space2 = (space2_1 < space2_2) ? space2_1 : space2_2;
            if (space2_1 == -1)
            {
                space2 = space2_2;
            }
            if (space2_2 == -1)
            {
                space2 = space2_1;
            }

            var atofx;

            if (space2 < 0)
            {
                atofx = ato;
                space2 = SearchWord.length;
            }
            else
            {
                atofx = ato.substring(0, space2);
            }
            var single_word = maefx + atofx;
            SearchWord = strIns(SearchWord, space1 + 1, "\"");
            SearchWord = strIns(SearchWord, space2 + i + 1, "\"");

            document.activeElement.value = SearchWord;
            IsRightAfterAddquote = true;
        }
    }

    return false;
}
独習JavaScript 新版

コメント

タイトルとURLをコピーしました
inserted by FC2 system