dotfiles/conkeror/.conkerorrc/site-js/scuttle.js

128 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2016-09-28 14:39:42 +02:00
define_hook('scuttle_bookmarked_hook');
define_variable("scuttle_username", null,
"Username of te scuttle account.");
define_variable("scuttle_password", null,
"Password of the scuttle account.");
define_variable("scuttle_url", null,
"The URL where scuttle can be found.");
function scuttle_parse_xml(text)
{
var parser = Components
.classes["@mozilla.org/xmlextras/domparser;1"]
.createInstance(Components.interfaces.nsIDOMParser);
return parser.parseFromString(text, 'text/xml');
}
function scuttle_get_url(path)
{
let url = make_uri(scuttle_url);
if (scuttle_username != null)
url.username = scuttle_username;
if (scuttle_password != null)
url.password = scuttle_password;
url.path += '/api';
return url.spec + '/' + path;
}
function scuttle_send_url(I, obj, default_name) {
var postlinkurl;
var uri_string = load_spec_uri_string(load_spec(obj));
var panel = create_info_panel(
I.window, 'scuttle-panel',
[['scuttle-bookmarking', 'Sending URL to Scuttle: ', uri_string]]
);
try {
let description = yield I.minibuffer.read(
$prompt = "name (required): ",
$initial_value = default_name
);
let tags = yield I.minibuffer.read(
$prompt = "tags (space delimited): "
);
let extended = yield I.minibuffer.read(
$prompt = "extended description: "
);
postlinkurl = scuttle_get_url(
'posts_add.php?&url=' + escape(uri_string) +
'&description=' + escape(description) +
'&tags=' + escape(tags) +
'&extended=' + escape(extended)
);
} finally {
panel.destroy();
}
try {
var content = yield send_http_request(
load_spec({uri: postlinkurl}));
var dom = scuttle_parse_xml(content.responseText);
var result = dom.getElementsByTagName('result')[0];
var code = result.getAttribute('code');
I.window.minibuffer.message(code);
if (code === 'done')
scuttle_bookmarked_hook.run();
} catch (e) { }
}
2016-09-28 14:39:42 +02:00
function scuttle_bookmarked_widget(window)
{
this.class_name = 'scuttle-bookmark-widget';
text_widget.call(this, window);
this.add_hook('current_content_buffer_location_change_hook');
this.add_hook('select_buffer_hook');
this.add_hook('scuttle_bookmarked_hook');
}
scuttle_bookmarked_widget.prototype = {
constructor: scuttle_bookmarked_widget,
__proto__: text_widget.prototype,
update: function () {
var obj = this;
function doit() {
check_buffer(obj.window.buffers.current, content_buffer);
let posturl = scuttle_get_url('is_bookmarked.php?url=' +
encodeURIComponent(
load_spec_uri_string(
load_spec(obj.window.buffers.current.top_frame)
)
));
try {
let content = yield send_http_request(
load_spec({uri: posturl})
);
var dom = scuttle_parse_xml(content.responseText);
var result = dom.getElementsByTagName('result')[0].innerHTML;
obj.view.text = result == 'true'
? "+" : "-";
}
catch (e) { }
}
co_call(doit());
}
};
interactive("scuttle-post",
"bookmark the page via scuttle",
function (I) {
check_buffer(I.buffer, content_buffer);
co_call(scuttle_send_url(I, I.buffer.top_frame, I.buffer.title));
2016-09-28 14:39:42 +02:00
});
interactive("scuttle-post-link",
"bookmark the link via scuttle",
function (I) {
let bo = yield read_browser_object(I);
2016-09-28 14:39:42 +02:00
check_buffer(I.buffer, content_buffer);
co_call(scuttle_send_url(I, encodeURIComponent(bo), bo.textContent));
2016-09-28 14:39:42 +02:00
}, $browser_object = browser_object_links);