Skip to content

Commit dee4ff6

Browse files
committed
first commit.
0 parents  commit dee4ff6

9 files changed

+344
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

build.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
find fuhyohigai-ext -type f -name "*" | xargs zip dist/fuhyohigai-ext.zip

fuhyohigai-ext/core.js

+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
(function(undefined) {
2+
window.fuhyohigai = window.fuhyohigai || {};
3+
4+
var Asserts = {
5+
isNotEmptyString: function(source) {
6+
if (!source || typeof source !== 'string') {
7+
throw new Error('え、それは…(困惑): source -> ' + source);
8+
}
9+
}
10+
, isFunction: function(source) {
11+
if (!source || typeof source !== 'function') {
12+
throw new Error('え、それは…(困惑): source -> ' + source);
13+
}
14+
}
15+
};
16+
17+
var Entry = (function() {
18+
function Entry(key, value) {
19+
this.key = key;
20+
this.value = value;
21+
}
22+
return Entry;
23+
})();
24+
25+
var Holder = (function() {
26+
function Holder() {
27+
this._entries = [];
28+
}
29+
30+
Holder.prototype.put = function(key, value) {
31+
this._entries.push(new Entry(key, value));
32+
};
33+
34+
Holder.prototype.contains = function(key, value) {
35+
var entriesLength = this._entries.length
36+
, i = 0
37+
, entry = null;
38+
for (; i < entriesLength; i++) {
39+
entry = this._entries[i];
40+
if (entry.key === key && entry.value === value) {
41+
return true;
42+
}
43+
}
44+
return false;
45+
};
46+
47+
Holder.prototype.containsKey = function(key) {
48+
var entriesLength = this._entries.length
49+
, i = 0
50+
, entry = null;
51+
for (; i < entriesLength; i++) {
52+
entry = this._entries[i];
53+
if (entry.key === key) {
54+
return true;
55+
}
56+
}
57+
return false;
58+
};
59+
60+
Holder.prototype.containsValue = function(value) {
61+
var entriesLength = this._entries.length
62+
, i = 0
63+
, entry = null;
64+
for (; i < entriesLength; i++) {
65+
entry = this._entries[i];
66+
if (entry.value === value) {
67+
return true;
68+
}
69+
}
70+
return false;
71+
};
72+
73+
Holder.prototype.remove = function(key, value) {
74+
var entriesLength = this._entries.length
75+
, i = entriesLength - 1
76+
, entry = null;
77+
for (; i >= 0; i--) {
78+
entry = this._entries[i];
79+
if (entry.key === key && entry.value === value) {
80+
this._entries.splice(i, 1);
81+
return;
82+
}
83+
}
84+
};
85+
86+
Holder.prototype.findValuesByKey = function(key) {
87+
var values = []
88+
, entriesLength = this._entries.length
89+
, i = 0
90+
, entry = null;
91+
for (; i < entriesLength; i++) {
92+
entry = this._entries[i];
93+
if (entry.key === key) {
94+
values.push(entry.value);
95+
}
96+
}
97+
return values;
98+
};
99+
100+
return Holder;
101+
})();
102+
103+
var Core = (function() {
104+
105+
function Core() {
106+
this.interval = 60;
107+
this._timer = null;
108+
this._hostRegExps = [];
109+
this._listenerHolder = new Holder();
110+
}
111+
112+
Core.prototype._dispatch = function() {
113+
var currentHost = window.location.host
114+
, hostsMatched = []
115+
, hostRegExpsLength = this._hostRegExps.length
116+
, i = 0
117+
, hostRegExp = null;
118+
for (; i < hostRegExpsLength; i++) {
119+
hostRegExp = this._hostRegExps[i];
120+
if (hostRegExp.test(currentHost)) {
121+
hostsMatched.push(hostRegExp.source);
122+
}
123+
}
124+
125+
i = 0;
126+
var listeners = []
127+
, hostsMatchedLength = hostsMatched.length
128+
, hostMatched = null;
129+
for (; i < hostsMatchedLength; i++) {
130+
hostsMatched = hostsMatched[i];
131+
var lists = this._listenerHolder.findValuesByKey(hostsMatched);
132+
Array.prototype.push.apply(listeners, lists);
133+
}
134+
135+
i = 0;
136+
var listenersLength = listeners.length
137+
, listener = null;
138+
for (; i < listenersLength; i++) {
139+
listener = listeners[i];
140+
listener.call();
141+
}
142+
};
143+
144+
Core.prototype._onInsertDomNode = function(event) {
145+
if (this._timer) { return; }
146+
this._timer = setTimeout((function() {
147+
this._dispatch();
148+
this._timer = null;
149+
}).bind(this), this.interval);
150+
}
151+
152+
Core.prototype.start = function() {
153+
window.document.addEventListener('DOMNodeInserted', this._onInsertDomNode.bind(this));
154+
};
155+
156+
Core.prototype.stop = function() {
157+
window.document.removeEventListener('DOMNodeInserted', this._onInsertDomNode.bind(this));
158+
if (this._timer) {
159+
clearTimeout(this._timer);
160+
this._timer = null;
161+
}
162+
};
163+
164+
Core.prototype.addListener = function(host, callback) {
165+
Asserts.isNotEmptyString(host);
166+
Asserts.isFunction(callback);
167+
if (this._listenerHolder.contains(host, callback)) {
168+
return;
169+
}
170+
this._listenerHolder.put(host, callback);
171+
172+
var hostRegExpsLength = this._hostRegExps.length
173+
, i = 0
174+
, hostRegExp = null;
175+
for (; i < hostRegExpsLength; i++) {
176+
hostRegExp = this._hostRegExps[i];
177+
if (hostRegExp.source === host) {
178+
return;
179+
}
180+
}
181+
this._hostRegExps.push(new RegExp(host));
182+
};
183+
184+
Core.prototype.contains = function(host, callback) {
185+
return this._listenerHolder.contains(host, callback);
186+
};
187+
188+
Core.prototype.removeListener = function(host, callback) {
189+
this._listenerHolder.remove(host, callback);
190+
if (this._listenerHolder.findValuesByKey(host).length) {
191+
return;
192+
}
193+
var hostRegExpsLength = this._hostRegExps.length
194+
, i = hostRegExpsLength - 1
195+
, hostRegExp = null;
196+
for (; i >= 0; i--) {
197+
var hostRegExp = this._hostRegExps[i];
198+
if (hostRegExp.source === host) {
199+
this._hostRegExps.splice(i, 1);
200+
}
201+
}
202+
};
203+
204+
return Core;
205+
})();
206+
207+
window.fuhyohigai.core = new Core();
208+
}).call(this);

fuhyohigai-ext/facebook.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(function(undefined) {
2+
function $replaceHtml($obj, target, replacement) {
3+
var html = $obj.html();
4+
html = html.replace(new RegExp(target, "g"), replacement);
5+
$obj.html(html);
6+
}
7+
8+
function createTagContainsLike(tag, source) {
9+
return tag + ':contains("' + source + '")';
10+
};
11+
12+
$(function() {
13+
var lang = fuhyohigai.langpack
14+
, xxLike = lang.xx.like
15+
, currentLike = null;
16+
17+
switch ($('html').attr('lang')) {
18+
case 'en':
19+
currentLike = lang.en.like;
20+
break;
21+
default:
22+
currentLike = lang.ja.like;
23+
break;
24+
}
25+
26+
var targets = [
27+
createTagContainsLike('a', currentLike)
28+
, createTagContainsLike('label', currentLike)
29+
, createTagContainsLike('span', currentLike)
30+
, createTagContainsLike('h5', currentLike)
31+
];
32+
33+
fuhyohigai.onashasu('www.facebook.com', function() {
34+
for (var i = 0; i < targets.length; i++) {
35+
var target = targets[i];
36+
$(target).each(function() {
37+
$replaceHtml($(this), currentLike, xxLike);
38+
});
39+
}
40+
});
41+
});
42+
}).call(this);

fuhyohigai-ext/fuhyohigai.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(function(undefined) {
2+
window.fuhyohigai.start();
3+
}).call(this);

fuhyohigai-ext/jquery-1.9.1.min.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuhyohigai-ext/langpack.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(function(undefined) {
2+
var lang = window.fuhyohigai.langpack = {};
3+
4+
lang.ja = {
5+
like: 'いいね!'
6+
};
7+
8+
lang.en = {
9+
like: 'Like'
10+
};
11+
12+
lang.xx = {
13+
like: 'あ^~いいっすね'
14+
};
15+
16+
17+
function createReversed(langpack, key) {
18+
var reversed = {};
19+
for (var lang in langpack) {
20+
reversed[lang] = langpack[lang][key];
21+
}
22+
return reversed;
23+
};
24+
25+
var like = createReversed(lang, 'like');
26+
// var hoge = createReversed(lang, 'hoge');
27+
28+
lang.like = like;
29+
// lang.hoge = hoge;
30+
}).call(this);

fuhyohigai-ext/manifest.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "fuhyohigai-ext",
3+
"version": "0.0.1",
4+
"manifest_version": 2,
5+
"description": "Japanese FuhyoHigai Extension",
6+
"content_scripts": [
7+
{
8+
"matches": [
9+
"*://*/*"
10+
],
11+
"js": [
12+
"jquery-1.9.1.min.js",
13+
"core.js",
14+
"langpack.js",
15+
"utils.js",
16+
"fuhyohigai.js",
17+
"facebook.js"
18+
],
19+
"run_at": "document_start"
20+
}
21+
]
22+
}

fuhyohigai-ext/utils.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(function(undefined) {
2+
var utils = window.fuhyohigai;
3+
var core = utils.core;
4+
5+
6+
utils.start = function() {
7+
return core.start();
8+
};
9+
10+
utils.stop = function() {
11+
return core.stop();
12+
};
13+
14+
utils.addListener = function(host, callback) {
15+
return core.addListener(host, callback);
16+
};
17+
18+
utils.contains = function(host, callback) {
19+
return core.contains(host, callback);
20+
};
21+
22+
utils.removeListener = function(host, callback) {
23+
return core.removeListener(host, callback);
24+
};
25+
26+
// alias
27+
utils.onashasu = utils.addListener;
28+
utils.ossu = utils.addListener;
29+
utils.ossuossu = utils.addListener;
30+
31+
}).call(this);

0 commit comments

Comments
 (0)