-
Notifications
You must be signed in to change notification settings - Fork 37
/
OperaStyle_Go_to_nickname.uc.js
163 lines (150 loc) · 4.73 KB
/
OperaStyle_Go_to_nickname.uc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// ==UserScript==
// @name OperaStyle_gotoNickname.uc.js
// @namespace http://d.hatena.ne.jp/Griever/
// @include main
// @charset UTF-8
// @varsion 0.0.10
// @note 0.0.10 Firefox Syncなどでブックマークが削除された時に動作しなかったのを修正
// @note 0.0.9 e10s に対応したかも
// @note 0.0.8 getKeywordForURI が廃止になるらしいので対応
// @note 0.0.7 Firefox 25 の getShortcutOrURI 廃止に仮対応
// @note 0.0.5 Remove E4X
// ==/UserScript==
/*
keyconfig 等で gotoNickname.open() を実行。
gotoNickname.open("google"); でも可能。
*/
(function () {
if (window.gotoNickname) {
window.gotoNickname.destroy();
delete window.gotoNickname;
}
window.gotoNickname= {
BEEP : false, //ビープ音を鳴らす
SHOW_COMPLETE: true, // 候補を表示
ALAWAYS_CHECK: false, // 毎回キーワードを取得し直す
lastMatchedKeyword: '',
get keywords(){
return this._keywords || (this._keywords = this.getKeywords());
},
get _beep() {
delete this._beep;
return this._beep = Cc["@mozilla.org/sound;1"].createInstance(Ci.nsISound);
},
beep: function() {
this._beep.beep();
},
getKeywords: function(){
var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService);
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
// http://www.xuldev.org/blog/?p=181
function flatChildNodes(aItemId) {
var ret = [];
var parentNode = PlacesUtils.getFolderContents(aItemId).root;
for (var i = 0; i < parentNode.childCount; i++) {
var childNode = parentNode.getChild(i);
if (PlacesUtils.nodeIsBookmark(childNode)){
// var uri = ios.newURI(childNode.uri, null, null);
if (!childNode.uri || childNode.uri.indexOf('%s') >= 0)
continue;
var keyword = bmsvc.getKeywordForBookmark(childNode.itemId);
if (keyword) ret.push(keyword);
}
else if (PlacesUtils.nodeIsFolder(childNode))
// call this function recursive
ret = ret.concat(arguments.callee(childNode.itemId));
}
return ret;
}
return flatChildNodes(1).filter(function(e, i, a) a.indexOf(e) === i);
},
open: function(str){
if (str){
this.loadKeyword(str);
return;
}
this.panel.openPopupAtScreen(
window.screenX + window.innerWidth/2 - 150
, window.screenY + window.innerHeight/2 - 40
, false
);
if (this.ALAWAYS_CHECK)
this._keywords = null;
},
onPopupshown: function(event) {
this.input.inputField.style.imeMode = "inactive";
this.input.select();
if (this.BEEP) this.beep();
},
onPopuphidden: function(event) {
this.input.value = this.lastMatchedKeyword;
this.label.value = '';
},
onInput: function(event) {
var value = this.input.value;
if (value == '') {
this.label.value = '';
return;
}
var matchKeywords = this.keywords.filter(function(e) e.indexOf(value) === 0);
if (matchKeywords.length === 1) {
this.loadKeyword(matchKeywords[0]);
this.lastMatchedKeyword = matchKeywords[0];
this.panel.hidePopup();
} else if (this.SHOW_COMPLETE) {
this.label.value = matchKeywords.join(' ');
}
},
onKeypress: function(event) {
var {keyCode:k, charCode:w, ctrlKey:c, shiftKey:s, altKey:a} = event;
if (k === event.DOM_VK_RETURN && !c && !s && !a) {
this.loadKeyword(this.input.value);
this.panel.hidePopup();
}
},
init: function(){
this.panel = $('mainPopupSet').appendChild($C("panel", {
id: "gotoNickname-panel",
width: "300",
height: "80",
onpopupshown: "gotoNickname.onPopupshown(event);",
onpopuphidden: "gotoNickname.onPopuphidden(event);",
style: "background-color: -moz-dialog; padding: 10px;"
}));
this.panel.appendChild($C("description", {
value: "ブックマークのキーワードを入力してください",
}));
this.input = this.panel.appendChild($C("textbox", {
id: "gotoNickname-input",
onkeypress: "gotoNickname.onKeypress(event)",
oninput: "gotoNickname.onInput(event);",
}));
this.label = this.panel.appendChild($C("description", {
id: "gotoNickname-label",
crop: "end",
value: "",
}));
function $(id) document.getElementById(id);
function $C(name, attr) {
var el = document.createElement(name);
if (attr) Object.keys(attr).forEach(function(n) el.setAttribute(n, attr[n]));
return el;
}
},
destroy: function() {
var elem = document.getElementById('gotoNickname-panel');
if (elem) elem.parentNode.removeChild(elem);
},
loadKeyword: function(keyword) {
PlacesUtils.keywords.fetch(keyword).then((obj) => {
var url = obj.url.href;
if (gBrowser.mCurrentTab.pinned && !url.startsWith('javascript')) {
openNewTabWith(url)
} else {
loadURI(url);
}
});
}
};
gotoNickname.init();
})();