Skip to content

Instantly share code, notes, and snippets.

@nazoking
Last active July 12, 2019 02:25
Show Gist options
  • Save nazoking/765a16ddca53c0dda8d5141dac355c71 to your computer and use it in GitHub Desktop.
Save nazoking/765a16ddca53c0dda8d5141dac355c71 to your computer and use it in GitHub Desktop.
aws_amazon_saml.userScript.js
// ==UserScript==
// @name AWS SAML
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://signin.aws.amazon.com/saml
// @grant none
// ==/UserScript==
(function() {
'use strict';
var input = $('<input type="text" />').appendTo("form p");
var map = new Map();
var list = $("div.saml-account:not([id])");
list.each((i,e) => map.set(e, {
text:$(e).find(".saml-account-name").text().trim().replace(/^Account:\s+/,""),
index:i,
score:{}
}));
var old = "";
input.keyup(e=>{
var text = e.target.value;
if(text == old) return;
old=text;
if(text==""){
list.sort((a,b) => map.get(a).index < map.get(b).index ? -1 : 1);
$("fieldset").html("").append(list);
}else{
Array.from(map.values()).forEach((v) => v.score=string_score(v.text, text));
const l2 = list.filter((i, a) => map.get(a).score.score > 0)
l2.sort((a,b) => {
return map.get(a).score.score > map.get(b).score.score ? -1 : 1;
});
$("fieldset").html("").append(l2);
}
});
input.focus();
function getHistory(){
try{
const x = JSON.parse(localStorage.getItem('filter-history'));
if(x instanceof Array){
return x.map(x => `${x}`.trim()).filter(x => x);
}else{
return [];
}
}catch(e){
}
return [];
}
$("#signin_button").click((e) => {
var txt = input.val().trim();
if(txt){
var hist = getHistory();
hist = [txt, ...hist.filter(a => a == txt)];
localStorage.setItem('filter-history', JSON.stringify(hist));
}
});
$("<select>").append(new Option("履歴",""), getHistory().map(v => new Option(v))).appendTo("form p").change((e) => {
const x = $(e.target).val();
if(x){
input.val(x);
input.keyup();
}
});
/*!
* string_score.js: String Scoring Algorithm 0.1.22
*
* http://joshaven.com/string_score
* https://github.com/joshaven/string_score
*
* Free to use, modify, etc... see attached license for more info
* Copyright (C) 2009-2015 Joshaven Potter <yourtech@gmail.com>
* MIT License: http://opensource.org/licenses/MIT
* Special thanks to all of the contributors listed here https://github.com/joshaven/string_score
*
* Updated: Tue Mar 10 2015
*/
function string_score(string, word) {
'use strict';
var zero = {score:0,matchingPositions:[]};
// If the string is equal to the word, perfect match.
if (string === word || word === "") { return {score:1, matchingPositions:[]}; }
var lString = string.toUpperCase(),
strLength = string.length,
lWord = word.toUpperCase(),
wordLength = word.length;
return calc(zero, 0, 0, 0, 0, []);
function calc(score, startAt, skip, runningScore, i, matchingPositions){
if( i < wordLength) {
var charScore = 0;
// Find next first case-insensitive match of a character.
var idxOf = lString.indexOf(lWord[i], skip);
if (-1 === idxOf) { return score; }
score = calc(score, startAt, idxOf+1, runningScore, i, matchingPositions);
if (startAt === idxOf) {
// Consecutive letter & start-of-string Bonus
charScore = 0.8;
} else {
charScore = 0.1;
// Acronym Bonus
// Weighing Logic: Typing the first character of an acronym is as if you
// preceded it with two perfect character matches.
if (/^[^A-Za-z0-9]/.test(string[idxOf - 1])){
charScore += 0.7;
}else if(string[idxOf]==lWord[i]) {
// Upper case bonus
charScore += 0.2;
// Camel case bonus
if(/^[a-z]/.test(string[idxOf - 1])){
charScore += 0.5;
}
}
}
// Same case bonus.
if (string[idxOf] === word[i]) { charScore += 0.1; }
// next round
return calc(score, idxOf + 1, idxOf + 1, runningScore + charScore, i+1, matchingPositions.concat(idxOf));
}else{
// skip non match folder
var effectiveLength = strLength;
if(matchingPositions.length){
var lastSlash = string.lastIndexOf('/',matchingPositions[0]);
if(lastSlash!==-1){
effectiveLength = strLength-lastSlash;
}
}
// Reduce penalty for longer strings.
var finalScore = 0.5 * (runningScore / effectiveLength + runningScore / wordLength);
if ((lWord[0] === lString[0]) && (finalScore < 0.85)) {
finalScore += 0.15;
}
if(score.score >= finalScore){
return score;
}
return {score:finalScore, matchingPositions:matchingPositions};
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment