Twitter에서 멘션된 유저만 뽑아오기

트위터가 제공하는 API에서 유저가 쓴 글은 당연히 불러올 수 있지만 그 글에 멘션 된 유저는 불러올 수 없다. 그렇다고 그냥 거기에 스레드를 생성한다고 자동으로 멘션이 붙는 것도 아니다. 그래서 결국은 멘션된 유저의 목록을 뽑아 올 필요가 있다.

트위터에서는 @만 붙인다고 다 멘션이 되는 게 아니다. 예를 들어 @a-@b 가@is나 a@not과 같은 트윗이 있다고 하자.
이 경우 a, b, is는 멘션이 된 유저지만 not의 경우 앞에 문자([a-zA-Z_])가 있기 때문에 멘션이 되지 않는다. 그래서 1시간 동안 씨름 한 결과 이걸 정확히 걸러내는 정규식을 만들었다.

(?:\B@)\w+

위와 같이 사용하면 정확히 멘션이 된 유저만 뽑아 올 수 있다. 다만 @a @b @a 같은 트윗일 경우 중복이 될 수 있으니 중복 제거는 알아서 해야 한다.

[userscript] remove ‘ㅋ’ in facebok and twitter

For facebook:

// ==UserScript==
// @name remove ㅋ
// @namespace Kjwon15
// @description remove ㅋ in any page
// @include http://*.facebook.com/*
// @include https://*.facebook.com/*
// ==/UserScript==

removeFunc = function(){
	var divs = document.querySelectorAll('div.storyContent');
	var count = 0;
	for(var i=0; i<divs.length; i++){
		count += (divs[i].innerHTML.match(/ㅋ/g)||[]).length;
		divs[i].innerHTML = divs[i].innerHTML.replace(/ㅋ/g, '');
	}
	if(count > 0){
		alert('removed ' + count + ' ㅋs');
		beeper(count);
	}
}

window.addEventListener('load', removeFunc, false);

//var button = document.createElement('button');
//button.innerHTML = 'click';
//button.style.cssText = "position: fixed; top: 0; left: 0; z-index: 9999; background-color: #808080;";
//button.onclick = removeFunc;
//document.body.appendChild(button);

For twitter

// ==UserScript==
// @name remove ㅋ at twitter
// @namespace Kjwon15
// @description remove ㅋ in twitter
// @include http://twitter.com/*
// @include https://twitter.com/*
// ==/UserScript==

removeFunc = function(){
	var count = 0;
	var tweets = document.querySelectorAll("p.js-tweet-text");
	for(var i=0; i<tweets.length; i++){
		count += (tweets[i].innerHTML.match(/ㅋ/g)||[]).length;
		tweets[i].innerHTML = tweets[i].innerHTML.replace(/ㅋ/g, '');
	};
	if(count > 0){
		alert('removed ' + count + ' ㅋs');
	}
	return false;
}

window.addEventListener('load', removeFunc, false);

var li = document.createElement('li');
var button = document.createElement('a');
button.id = 'kuchen';
button.href = "/";
button.innerHTML = '<span>remove</span>';
button.className = "js-nav";
//button.style.cssText = "display: block; height: 12px; color: #00a7e1; padding: 13px 12px 15px; font-weight: bold; line-height: 1;";
button.onclick = removeFunc;
li.appendChild(button);
document.querySelector('ul#global-actions').appendChild(li);