AtCoderUserSearchForm

add form for user search (only lang=ja)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AtCoderUserSearchForm
// @namespace    https://github.com/refine-P
// @version      1.0.2
// @description  add form for user search (only lang=ja)
// @author       fine
// @license      MIT
// @include      https://atcoder.jp/home*
// @include      https://atcoder.jp/users*
// ==/UserScript==

/*
    JavaScriptをまともに書いたことがない人間が書いたので、
    気になる所があれば、Twitterとかで教えていただければ幸いです。
*/

"use strict";

// ユーザ検索のフォームを挿入する
// http://kimizuka.hatenablog.com/entry/2014/09/12/095957
var addUserSearchHTML = [
'<form class="navbar-form navbar-left">',
'   <div class="form-group input-group input-group-sm" style="margin-top:3px;">',
'       <input type="text" class="form-control" id="user_search", placeholder="User Search">',
'       <div class="input-group-btn">',
'           <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>',
'       </div>',
'    </div>',
'</form>'
].join("\n");

var target = document.getElementsByClassName('header-sub_page')[0].lastElementChild;
target.insertAdjacentHTML('afterend', addUserSearchHTML);

// ユーザ検索の項目を取得
var userSearch = document.getElementsByClassName('header-sub_page')[0].getElementsByTagName('form')[0];

// 言語に応じてplaceholderを変更
var lang = document.getElementsByTagName('meta')[1].content; // 使用言語を取得
if (lang == 'ja') userSearch.getElementsByTagName('input')[0].placeholder = 'ユーザ検索';

// ユーザ検索機能の追加
userSearch.addEventListener('submit', function(event) {
    event.preventDefault();

    var base = "https://atcoder.jp/users/";
    var userName = userSearch.user_search.value;
    var url = base + userName;

    // ユーザーがいるかどうかチェックして、
    // いなかったらページをいい感じにするやつがやりたいが一旦保留
    // とりあえず、何も考えずにユーザー名をくっつけて遷移
    window.location.href = url;
}, false);