-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinboard.js
173 lines (147 loc) · 5.85 KB
/
pinboard.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
164
165
166
167
168
169
170
171
172
173
/**
* pinboard.js -- A Pinboard page-mode for Conkeror
*
* (C) Copyright 2011 Leonardo Etcheverry <leo@kalio.net>
*
* Usage: use j,k to move to the next/previous bookmark. RET follows
* the selected bookmark. You should disable Pinboard's builtin key
* shortcuts (under settings). They clash with Conkeror anyway.
*
* TODO:
* - Implement update commands (star, edit tags, etc.)
* - Allow to opt between cirular bookmark motion and automatic next/prev page navigation
* - Use a different color or some better way to highlight private bookmarks.
*
**/
require("content-buffer.js");
/* default Pinboard keys (IGNORE THIS FOR NOW).
*
* Since Pinboard uses keydown handlers directly, the site builtin
* keyboard shortcuts are triggered even without enabling passthrough
* in pinboard-mode-keymap.
*
* We keep this list for documentation purposes only.
*
fallthrough_keys = ["?", // show list of commands
"/", // set focus in searchbox
"b", // bulk edit
"j", // earlier
"k", // later
"g a", // homepage
"g n", // network
"g s", // settings
"g t", // tweets
"g u", // unread
]
*/
/*
* User stylesheet
* Change highlight colors here.
*/
register_user_stylesheet(
"data:text/css," +
escape (
"@-moz-document url-prefix(https://pinboard.in/) {" +
".current {" +
" background-color: #FFFFCC !important;" +
" // border: 1px dotted #C41 !important;"+
" border-right: 2px solid #C41 !important;"+
"}" +
".private {" +
" background: #ddd !important;" +
"}" +
"}"
));
/*
* Select the next bookmark item in a circular fashion, according to
* `direction'. Selecting a bookmark means adding the class "current"
* to the selected bookmark element.
*/
function _pinboard_next(I, direction) {
var bookmarks = Array.filter(I.buffer.document.getElementsByClassName("bookmark"), function (b) { return true });
var cb = bookmarks.filter(function (b) { return (b.className.indexOf("current") >= 0); });
var current = (cb.length != 0) ? cb[0] : bookmarks[bookmarks.length-1] ;
var nexti = (bookmarks.indexOf(current) + direction) % bookmarks.length
var next = bookmarks[nexti]
if (current)
dom_remove_class(current, "current");
dom_add_class(next, "current");
return next;
}
function _pinboard_focus_selected(I, el) {
var a = I.buffer.document.evaluate(
'//*[contains(@class,"current")]//a[contains(@class,"bookmark_title")]',
el, null, Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null);
// focus the bookmark's link anchor
browser_set_element_focus(I.buffer, a.singleNodeValue, false);
// scroll into view if necessary
var boundRect = el.getBoundingClientRect();
var win = I.buffer.focused_frame
if (boundRect.top < 0 || boundRect.bottom > win.innerHeight)
el.scrollIntoView();
}
/*
* _on_pinboard_load will be called by the page-mode's $enable callback
*/
function _on_pinboard_load(buffer)
{
_pinboard_fix_link_rel(buffer);
}
/**
* Add link rel="next" and rel="prev" to "later" and "earlier" links
* TODO: This should not be necessary once pinboard.in fixes its html.
*/
function _pinboard_fix_link_rel(buffer)
{
var prev_link = buffer.document.getElementById("top_earlier");
var next_link = buffer.document.getElementById("top_later");
if (prev_link) prev_link.setAttribute("rel", "prev");
if (next_link) next_link.setAttribute("rel", "next");
}
/*
* Interactive commands
*/
interactive("pinboard-next-bookmark",
"Highlight next pinboard bookmark",
function (I) { _pinboard_focus_selected(I, _pinboard_next(I, 1 /* down */)); });
interactive("pinboard-prev-bookmark",
"Highlight prev pinboard bookmark",
function (I) { _pinboard_focus_selected(I, _pinboard_next(I, -1 /* up */)); });
/*
* keybindings
*/
define_keymap("pinboard_keymap", $display_name = "pinboard");
define_key(pinboard_keymap, "j", "pinboard-next-bookmark");
define_key(pinboard_keymap, "k", "pinboard-prev-bookmark");
// Ignore this for now, see fallthrough_keys comment
// fallthrough_keys.map(function (key){define_key(pinboard_keymap, key, null, $fallthrough);});
var pinboard_modality = {
normal: pinboard_keymap
};
define_page_mode("pinboard_mode",
build_url_regexp($domain = "pinboard",
$allow_www = true,
$tlds = ["in"]),
function enable(buffer) {
buffer.content_modalities.push(pinboard_modality);
if (buffer.browser.webProgress.isLoadingDocument)
/* arrange so that _on_pinboard_load callback is
called once the document is done loading. */
add_hook.call(buffer, "buffer_loaded_hook", _on_pinboard_load);
else
/* call it right now in case the page-mode
* document is already loaded (ie, someone M-x
* pinboard-mode manually) */
_on_pinboard_load(buffer);
},
function disable(buffer) {
// unregister hooks
remove_hook.call(buffer, "buffer_loaded_hook", _on_pinboard_load);
var i = buffer.content_modalities.indexOf(pinboard_modality);
if (i > -1)
buffer.content_modalities.splice(i, 1);
},
$display_name = "Pinboard",
$doc = "Pinboard page-mode: navigation for Pinboard bookmarks." );
page_mode_activate(pinboard_mode);
provide("pinboard");