Skip to content

Instantly share code, notes, and snippets.

@ronekko
Created October 20, 2009 18:18
Show Gist options
  • Save ronekko/214477 to your computer and use it in GitHub Desktop.
Save ronekko/214477 to your computer and use it in GitHub Desktop.
// 以下のFirefox用アドオンにタグ選択パネルを付加するuserChrome.jsスクリプトです
// Hatebu IncSearch, livedoor clip IncSearch, del.icio.us IncSearch, Google Bookmarks IncSearch
(function(){
const LIVEDOOR_URL = 'chrome://livedoorclip_incsearch/content/view.html';
const LIVEDOOR_DIR = 'livedoorclip_incsearch';
const HATEBU_URL = 'chrome://hatebu_incsearch/content/view.html';
const HATEBU_DIR = 'hatebu_incsearch';
const DELICIOUS_URL = 'chrome://delicious_incsearch/content/view.html';
const DELICIOUS_DIR = 'delicious_incsearch';
const GOOGLE_URL = 'chrome://googlebookmarks_incsearch/content/view.html';
const GOOGLE_DIR = 'googlebookmarks_incsearch';
var textBox;
var init = function(doc, dirName){
var tagView;
var tagList;
var hideButton;
var statement;
var tags = [];
var tagListDisplay = true;
// 登録されている全てのタグを取得
statement = createStatement(dirName);
while (statement.step()) {
if(statement.row['tags']){
tags = tags.concat(statement.row['tags'].replace(/\[(.+?)\]/g, '$1').split(' '));
}
}
statement.reset();
tags = uniq(tags).sort();
textBox = doc.getElementById('text');
tagView = doc.createElement('div');
tagView.style.marginLeft = '150px';
tagList = createTagList(doc);
for(var i=0; i<tags.length; i+=1){
tagList.appendChild(createTagElement(tags[i]));
}
hideButton = createHideButton(doc);
tagView.appendChild(hideButton);
tagView.appendChild(tagList);
doc.body.insertBefore(tagView, doc.getElementById('viewHeader'));
hideButton.addEventListener('click', function(){
tagList.style.display = hideButton.stateDisplay ? 'none' : '';
hideButton.toggleDisplay();
}, true);
textBox.addEventListener('focus', function(){
tagView.style.display = 'block';
}, true);
}
var createTagList = function(doc){
var list = doc.createElement('div');
list.id = 'tagList';
list.style.border = 'solid';
list.style.borderWidth = 'thin';
list.style.borderColor = '#737373';
list.style.width = '500px';
list.style.paddingTop = '3px';
list.style.paddingBottom = '3px';
list.style.paddingLeft = '10px';
list.style.paddingRight = '5px';
list.style.fontSize = '13px';
return list;
}
var createTagElement = function(tagName){
var state = false;
var span = document.createElement('span');
var text = document.createTextNode(tagName);
span.appendChild(text);
span.style.marginRight = '7px';
span.style.marginBottom = '3px';
span.style.cursor = 'pointer';
span.addEventListener('click', function(evt){
state = !state;
if(state){
textBox.value += tagName + ' ';
span.style.backgroundColor = '#0000FF';
span.style.color = '#FFFFFF';
}else{
textBox.value = textBox.value.replace(tagName + ' ', '');
span.style.backgroundColor = '#FFFFFF';
span.style.color = '#000000';
}
}, true);
return span;
}
var createHideButton = function(doc){
var btn = doc.createElement('span');
btn.innerHTML = 'Hide';
btn.style.border = 'solid';
btn.style.borderWidth = 'thin';
btn.style.borderColor = '#373737';
btn.style.backgroundColor = '#CCCCCC';
btn.style.position = 'absolute';
btn.style.left = '671px';
btn.style.padding = '5px';
btn.style.fontSize = '14px';
btn.style.cursor = 'pointer';
btn.stateDisplay = true;
btn.toggleDisplay = function(){
if(this.stateDisplay){
this.innerHTML = 'Show';
this.stateDisplay = false;
}else{
this.innerHTML = 'Hide';
this.stateDisplay = true;
}
}
return btn;
}
var getService = function(cls, interface) {
return Components.classes[cls].getService(Components.interfaces[interface]);
}
function createStatement(dir) {
var file = getService('@mozilla.org/file/directory_service;1', 'nsIProperties')
.get('ProfD', Components.interfaces.nsIFile); // ProfDはプロファイルのディレクトリ
if (dir) {
file.append(dir);
if (!file.exists()) {
file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
}
}
file.append('bookmark.sqlite');
var connection = getService('@mozilla.org/storage/service;1', 'mozIStorageService').openDatabase(file);
var statement = connection.createStatement('SELECT tags FROM bookmark');
return statement;
}
// 配列のユニーク化
function uniq(a){
return Array.reduce(a, function(e, r, arr){
if(e.indexOf(r) == -1){
e.push(r);
}
return e;
}, []);
}
window.document.addEventListener('DOMContentLoaded', function(evt) {
var location = evt.target.location;
if (location == LIVEDOOR_URL) {
init(evt.target, LIVEDOOR_DIR);
}else if(location == HATEBU_URL) {
init(evt.target, HATEBU_DIR);
}else if(location == DELICIOUS_URL) {
init(evt.target, DELICIOUS_DIR);
}else if(location == GOOGLE_URL) {
init(evt.target, GOOGLE_DIR);
}
}, false);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment