Skip to content

Instantly share code, notes, and snippets.

@gyuque
Created November 7, 2012 14:54
Show Gist options
  • Save gyuque/4032055 to your computer and use it in GitHub Desktop.
Save gyuque/4032055 to your computer and use it in GitHub Desktop.
あいさつ自動で返すやつ
// ==UserScript==
// @name autopoke
// @namespace gyuque
// @description あいさつ
// @include https://www.facebook.com/pokes*
// @version 1
// @grant none
// ==/UserScript==
(function() {
'use strict';
var AutoIDs = {};
function setup() {
eachPokeBoxes(putButton);
}
function eachPokeBoxes(func) {
var pokeBoxes = document.getElementsByClassName("pokeHeader");
var len = pokeBoxes.length;
for (var i = 0;i < len;++i) {
var box = pokeBoxes[i];
var pid = findUserID(box);
if (pid >= 0) {
func(box, pid);
}
}
}
function putButton(parent, pid) {
var btn = document.createElement("button");
btn.innerHTML = "autopoke";
btn.addEventListener("click", function(){
btn.disabled = "disabled";
btn.style.boxShadow = "0 0 2px 2px #ff0";
AutoIDs[pid] = true;
}, false);
parent.appendChild(btn);
}
function findUserID(parent) {
var ls = parent.childNodes;
for (var i = 0;i < ls.length;++i) {
var ch = ls[i];
if (ch.getAttribute) {
var attr = ch.getAttribute("data-hovercard");
if (attr) {
var pos = attr.indexOf("id=");
if (pos >= 0) {
return parseInt(attr.substring(pos + 3), 10);
}
}
}
}
return -1;
}
function observe() {
eachPokeBoxes(sendBack);
setTimeout(observe, 500);
}
function sendBack(parentBox, pid) {
if (!AutoIDs[pid]) {
return;
}
var ls = parentBox.parentNode.getElementsByTagName("a");
for (var i = 0;i < ls.length;++i) {
var a = ls[i];
var url = a.getAttribute("ajaxify");
if (url && url.indexOf("pokeback") >= 0) {
fireClick(a);
return;
}
}
}
function fireClick(el) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, unsafeWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);
}
setup();
observe();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment