Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name expand_fotolife_notation
// @description はてなダイアリー編集中にfotolife記法を展開する
// @namespace http://shiba-yu36.net/
// @include http://d.hatena.ne.jp/*
// ==/UserScript==
(function() {
var extention_map = {
g : 'gif',
j : 'jpg',
p : 'png'
};
function parseFotolifeNotation (string) {
console.log(string);
var fotolife_regexp = new RegExp("^\\[?f:id:([\\w-]+):(\\d+)(\\w):image(?::\\w+)?\\]?$");
if(string.match(fotolife_regexp)) {
return {
user_name : RegExp.$1,
initial : RegExp.$1.substr(0,1),
ext : extention_map[RegExp.$3],
fotolife_id : RegExp.$2
};
}
return "";
}
function showFotolifeTip (fotolife_notation, mouse_x, mouse_y) {
var img_url
= 'http://img.f.hatena.ne.jp/images/fotolife/'
+ fotolife_notation.initial + '/'
+ fotolife_notation.user_name + '/'
+ fotolife_notation.fotolife_id.substr(0, 8) + '/'
+ fotolife_notation.fotolife_id + '.'
+ fotolife_notation.ext;
console.log(img_url);
var img = document.getElementById('expand-fotolife-tip');
if (!img) {
img = document.createElement('img');
img.id = 'expand-fotolife-tip';
document.body.appendChild(img);
}
img.style.cssText
= 'position: absolute;'
+ 'top: ' + (mouse_y + 5) + ';'
+ 'left: ' + (mouse_x + 10) + ';'
+ 'max-height: 300px;'
+ 'background-color: #fffacd;'
+ 'border: solid 1px #000000;'
+ 'padding: 5px;';
img.src = img_url;
}
function expand_fotolife_notation (target, mouse_x, mouse_y) {
//初期化
var img = document.getElementById('expand-fotolife-tip');
if (img) {
img.src = '';
img.style.cssText = '';
}
if (!target.value || typeof(target.value.substring) !== 'function') {
return;
}
var select = target.value.substring(target.selectionStart, target.selectionEnd);
var fotolife_notation = parseFotolifeNotation(select);
if (!fotolife_notation) {
return;
}
showFotolifeTip(fotolife_notation, mouse_x, mouse_y);
}
// 実行するURIを制限
var allow_link1 = new RegExp('^http://d.hatena.ne.jp/\\w+(?:\\+\\w+)?/(?:\\d+(?:/\\d+)?)?(?:#edit_in_place)?$');
var allow_link2 = new RegExp('^http://d.hatena.ne.jp/\\w+(?:\\+\\w+)?/edit.*');
if (!location.href.match(allow_link1) && !location.href.match(allow_link2)) {
return;
}
document.addEventListener('mouseup', function (e) {
expand_fotolife_notation(e.target, e.pageX, e.pageY);
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment