☆button on anond
by
yuta25
2014-12-23 [2014/12/23 16:09:12]
はてな匿名ダイアリーに☆ボタン出します (for userscript)
@@ -2,96 +2,109 @@
* @title ☆button on anond
* @description はてな匿名ダイアリーに☆ボタン出します (for userscript)
* @include http://anond.hatelabo.jp*
- * @license MIT License
- * @require
+ * @contributor yuta25
+ * @contributor noromanba http://let.hatelabo.jp/noromanba/let/hLHVyMu855wo
+ * @license MIT License http://opensource.org/licenses/MIT
*/
+// c.f.
+// http://jsbeautifier.org/
+// http://www.jshint.com/
+// https://developer.mozilla.org/ja/docs/DOM/element.classList
+// http://archive.today/86e6T (404, but works well)
+
(function() {
- function main () {
- var iconizing = false;
- var setupStar = function() {
- var style = document.createElement('style');
- style.innerHTML = '.hatena-star-add-button-image{background:url(http://hatenablog.com/images/theme/star/hatena-star-add-button.png);}';
- document.querySelector('body').appendChild(style);
-
- if (!Hatena.Star)
- return;
-
- var entryConfig = {
- entryNodes: {
- // PC, article
- 'div.star-target': {
- uri: 'h3:first-child a:first-child',
- title: 'h3:first-child',
- container: 'h3:first-child'
- }
- }
- };
- Hatena.Star.SiteConfig = entryConfig;
+ var Hatena = window.Hatena;
+
+ function main() {
+ var ICONIZING = false;
+
+ var setupStar = function() {
+ var style = document.createElement('style');
+ style.textContent = '.hatena-star-add-button-image { background: url("http://hatenablog.com/images/theme/star/hatena-star-add-button.png"); }';
+ document.body.appendChild(style);
+
+ if (!Hatena.Star) return;
+
+ var entryConfig = {
+ entryNodes: {
+ // PC, article
+ 'div.star-target': {
+ uri: 'h3:first-child a:first-child',
+ title: 'h3:first-child',
+ container: 'h3:first-child'
+ }
+ }
+ };
+ Hatena.Star.SiteConfig = entryConfig;
+ };
+
+ var setupSection = function() {
+ var sections = document.querySelectorAll('.section');
+ Array.prototype.slice.call(sections).forEach(function(section) {
+ section.classList.add('star-target'); // fallback; node.className += ' class';
+ });
+ };
+
+ var iconize = function() {
+ ICONIZING = true;
+ var sections = document.querySelectorAll('div.section');
+ Array.prototype.slice.call(sections).forEach(function(section) {
+ if (section.querySelectorAll('.hatena-star-star-container').length == 0) return;
+ var starlinks = section.querySelectorAll('.hatena-star-star-container a:not([iconized])');
+ Array.prototype.slice.call(starlinks).forEach(function(link) {
+ link.setAttribute('iconized','');
+ link.style.width = '20px';
+ link.style.height = '20px';
+ link.style.position = 'relative';
+ link.style.display = 'inline-block';
+ link.style.margin = '0 1px 0 1px';
+
+ var user = link.pathname.replace('/', '');
+ var icon = document.createElement('img');
+ icon.src = 'http://cdn1.www.st-hatena.com/users/' + user.slice(0, 2) + '/' + user + 'profile.gif';
+ icon.style.height = '20px';
+ icon.style.position = 'absolute';
+ icon.style.width = '20px';
+
+ var star = link.querySelector('.hatena-star-star');
+ star.style.position = 'absolute';
+ star.style.bottom = '1px';
+ star.style.width = '10px';
+ link.insertBefore(icon, star);
+ });
+ section.classList.remove('star-target'); // or .toggle()
+ });
+ ICONIZING = false;
};
- setupSection = function() {
- var sections = document.querySelectorAll('.section');
- for (var i = 0, length = sections.length; i < length; i++) {
- if (!sections[i].querySelector('.hatena-star-star-container')) {
- sections[i].setAttribute('class', sections[i].getAttribute('class') + ' star-target');
- }
- }
+ var startPolling = function() {
+ var timer = 0;
+ document.addEventListener('DOMNodeInserted', function() {
+ if (timer) return;
+ timer = setTimeout(function() {
+ setupSection();
+ iconize();
+ if (!ICONIZING) Hatena.Star.EntryLoader();
+ timer = 0;
+ }, 500);
+ }, false);
};
- var iconize = function () {
- iconizing = true;
- var containers = document.querySelectorAll('.hatena-star-star-container');
- for (var i = 0, clength = containers.length; i < clength; i++) {
- var as = containers[i].querySelectorAll('a');
- for (var j = 0, alength = as.length; j < alength; j++) {
- if (as[j].hasAttribute('iconized')) continue;
- var user = as[j].href.replace('http://anond.hatelabo.jp/', '').replace('/', '');
- var icon = document.createElement('img');
- icon.src = 'http://cdn1.www.st-hatena.com/users/' + user.substr(0, 2) + '/' + user + '/profile.gif';
- icon.setAttribute('style', 'height:20px;position:absolute;width:20px;');
- var star = as[j].querySelector('.hatena-star-star');
- star.style['position'] = 'absolute';
- star.style['bottom'] = '1px';
- star.style['width'] = '10px';
- as[j].insertBefore(icon, star);
- as[j].setAttribute('iconized', '');
- as[j].style['width'] = '20px';
- as[j].style['height'] = '20px';
- as[j].style['position'] = 'relative';
- as[j].style['display'] = 'inline-block';
- as[j].style['margin'] = '0 1px 0 1px';
- }
- var section = containers[i].parentNode.parentNode;
- section.setAttribute('class', section.getAttribute('class').replace('star-target', ''));
- iconizing = false;
- }
- }
-
- var startPolling = function () {
- var timer = 0;
- document.addEventListener('DOMNodeInserted', function() {
- if(timer) return;
- timer = setTimeout(function() {
- setupSection();
- iconize();
- if (!iconizing)Hatena.Star.EntryLoader();
- timer = 0;
- }, 500);
- }, false);
- }
-
- var s = document.createElement('script');
- s.src = 'http://s.hatena.ne.jp/js/HatenaStar.js';
- s.type = 'text/javascript';
- s.addEventListener('load', function(){
- setupStar();
- setupSection();
- startPolling();
- }, false);
- document.querySelector('body').appendChild(s);
-}
-var script = document.createElement("script");
-script.textContent = "(" + main.toString() + ")();";
-document.body.appendChild(script);
-}());
+ // TBD basic exec w/ @run-at document-end
+ var s = document.createElement('script');
+ s.src = 'http://s.hatena.ne.jp/js/HatenaStar.js';
+ s.type = 'text/javascript';
+ s.addEventListener('load', function() {
+ setupStar();
+ setupSection();
+ startPolling();
+ }, false);
+ document.body.appendChild(s);
+ } // /main
+
+ // executeBrowserContext
+ var script = document.createElement('script');
+ script.textContent = '(' + main.toString() + '());';
+ document.body.appendChild(script);
+}());
/*
* @title ☆button on anond
* @description はてな匿名ダイアリーに☆ボタン出します (for userscript)
* @include http://anond.hatelabo.jp*
* @contributor yuta25
* @contributor noromanba http://let.hatelabo.jp/noromanba/let/hLHVyMu855wo
* @license MIT License http://opensource.org/licenses/MIT
*/
// c.f.
// http://jsbeautifier.org/
// http://www.jshint.com/
// https://developer.mozilla.org/ja/docs/DOM/element.classList
// http://archive.today/86e6T (404, but works well)
(function() {
var Hatena = window.Hatena;
function main() {
var ICONIZING = false;
var setupStar = function() {
var style = document.createElement('style');
style.textContent = '.hatena-star-add-button-image { background: url("http://hatenablog.com/images/theme/star/hatena-star-add-button.png"); }';
document.body.appendChild(style);
if (!Hatena.Star) return;
var entryConfig = {
entryNodes: {
// PC, article
'div.star-target': {
uri: 'h3:first-child a:first-child',
title: 'h3:first-child',
container: 'h3:first-child'
}
}
};
Hatena.Star.SiteConfig = entryConfig;
};
var setupSection = function() {
var sections = document.querySelectorAll('.section');
Array.prototype.slice.call(sections).forEach(function(section) {
section.classList.add('star-target'); // fallback; node.className += ' class';
});
};
var iconize = function() {
ICONIZING = true;
var sections = document.querySelectorAll('div.section');
Array.prototype.slice.call(sections).forEach(function(section) {
if (section.querySelectorAll('.hatena-star-star-container').length == 0) return;
var starlinks = section.querySelectorAll('.hatena-star-star-container a:not([iconized])');
Array.prototype.slice.call(starlinks).forEach(function(link) {
link.setAttribute('iconized','');
link.style.width = '20px';
link.style.height = '20px';
link.style.position = 'relative';
link.style.display = 'inline-block';
link.style.margin = '0 1px 0 1px';
var user = link.pathname.replace('/', '');
var icon = document.createElement('img');
icon.src = 'http://cdn1.www.st-hatena.com/users/' + user.slice(0, 2) + '/' + user + 'profile.gif';
icon.style.height = '20px';
icon.style.position = 'absolute';
icon.style.width = '20px';
var star = link.querySelector('.hatena-star-star');
star.style.position = 'absolute';
star.style.bottom = '1px';
star.style.width = '10px';
link.insertBefore(icon, star);
});
section.classList.remove('star-target'); // or .toggle()
});
ICONIZING = false;
};
var startPolling = function() {
var timer = 0;
document.addEventListener('DOMNodeInserted', function() {
if (timer) return;
timer = setTimeout(function() {
setupSection();
iconize();
if (!ICONIZING) Hatena.Star.EntryLoader();
timer = 0;
}, 500);
}, false);
};
// TBD basic exec w/ @run-at document-end
var s = document.createElement('script');
s.src = 'http://s.hatena.ne.jp/js/HatenaStar.js';
s.type = 'text/javascript';
s.addEventListener('load', function() {
setupStar();
setupSection();
startPolling();
}, false);
document.body.appendChild(s);
} // /main
// executeBrowserContext
var script = document.createElement('script');
script.textContent = '(' + main.toString() + '());';
document.body.appendChild(script);
}());
- Permalink
- このページへの個別リンクです。
- RAW
- 書かれたコードへの直接のリンクです。
- Packed
- 文字列が圧縮された書かれたコードへのリンクです。
- Userscript
- Greasemonkey 等で利用する場合の .user.js へのリンクです。
- Loader
- @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
- Metadata
- コード中にコメントで @xxx と書かれたメタデータの JSON です。