Skip to content

Instantly share code, notes, and snippets.

@foriequal0
Last active September 21, 2017 02:24
Show Gist options
  • Save foriequal0/d8837c06b3068300df6c6b1eed443178 to your computer and use it in GitHub Desktop.
Save foriequal0/d8837c06b3068300df6c6b1eed443178 to your computer and use it in GitHub Desktop.
Republic of Korea, Korea, South Korea, 대한민국, 한국, 남한 을 찾아서 자동으로 선택해줍니다
// ==UserScript==
// @name Do you know Kimchi
// @namespace do-you-know-kimchi.co.kr
// @description Republic of Korea, Korea, South Korea, 대한민국, 한국 을 찾아서 자동으로 선택해줍니다
// @include *
// @exclude file://*
// @version 1
// @grant none
// @run-at document-end
// @require https://cdn.rawgit.com/hiddentao/fast-levenshtein/fd716307/levenshtein.min.js
// ==/UserScript==
(function() {
const variants_threshold = {
"korea": 0,
"korea republic of": 2,
"korea south": 2,
"south korea": 2,
"republic of korea": 2,
"한국": 0,
"대한민국":0,
"남한": 0,
};
function index_of_nearest(options) {
let min_distance = Infinity;
let min_index = -1;
for(let i=0; i<options.length; i++) {
for(let variant in variants_threshold) {
let dist = window.Levenshtein.get(variant, options[i].value.toLowerCase());
if (dist <= variants_threshold[variant] && dist < min_distance) {
min_index = i;
}
dist = window.Levenshtein.get(variant, options[i].text.toLowerCase());
if (dist <= variants_threshold[variant] && dist < min_distance) {
min_index = i;
}
}
}
return min_index;
}
function select(root) {
for(var select of root.getElementsByTagName("select")) {
let index = index_of_nearest(select.options);
if (index != -1) {
select.selectedIndex = index;
}
};
}
select(document.body);
var observer = new MutationObserver(function(changes) {
for(var change of changes) {
select(change.target);
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();
@foriequal0
Copy link
Author

foriequal0 commented Jul 24, 2017

브라우저에 GreaseMonkey(Firefox) TamperMonkey(Chrome) 을 설치하고,
https://gist.github.com/foriequal0/d8837c06b3068300df6c6b1eed443178/raw/do-you-know-kimchi.user.js
위 링크를 누르면 자동으로 스크립트를 설치하겠느냐는 창이 뜰겁니다.

<select> 를 이용하지 않은 드롭다운이나, 폼이 동적생성되면 자동으로 작동하지 않을 수 있고 (잡아보려고 MutationObeserver 달아봤는데 일부 안잡히는 경우가 있는듯), 드롭다운과 다른 이벤트가 연동된 경우나, 변경을 가정하지 않은 숨겨진 폼 등이 있는 경우나, 다중선택 등의 경우에 페이지의 상태를 교란시켜 정상작동을 방해할수도 있고, DOM 변경이 잦은 페이지의 경우 성능을 저하시킬 수도 있고, 저도 모르게 빨래를 퍼먹을수도 있으니 그런 경우에는 끄세요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment