-
Notifications
You must be signed in to change notification settings - Fork 3
/
bookmarklet.js
44 lines (42 loc) · 1.27 KB
/
bookmarklet.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
javascript: (function() {
const q = location.href;
const p = document.title;
const s = window.getSelection().toString();
const obj = {
_type: 'addBookmarkOrComment',
url: document.location.href || '',
title: document.title || '',
comment: s,
quote: true,
rand: Math.random(),
};
const yamanote = 'http://localhost:3456';
const t = window.open(yamanote + '/popup', 'Yamanote', 'toolbar=no,width=200,height=200');
interval = setInterval(() => {
t.postMessage(JSON.stringify(obj), yamanote);
console.log('posted…')
}, 200);
if (window.__yamanote) {
return;
}
window.__yamanote = true;
window.addEventListener('message', (event) => {
if (event.origin === yamanote) {
clearInterval(interval);
console.log('event received from Yamanote');
const recv = JSON.parse(event.data);
if (recv.id && recv.htmlWanted) {
const bodyHtml = document.body.innerHTML;
const headHtml = document.head.innerHTML;
const obj = {
_type: 'addHtml',
id: recv.id,
html: `<head>${headHtml}</head>${bodyHtml}`,
};
t.postMessage(JSON.stringify(obj), yamanote);
}
} else {
console.log('unprocessed event received from ' + event.origin);
}
});
})()