60 lines
2.1 KiB
JavaScript
60 lines
2.1 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
// ── Page→Extension messaging (used by the snotes helper page) ─────────────
|
|
window.addEventListener('message', function (event) {
|
|
if (event.source !== window) return;
|
|
var msg = event.data;
|
|
if (!msg || typeof msg.kind !== 'string') return;
|
|
|
|
switch (msg.kind) {
|
|
case 'snotes-ping':
|
|
window.postMessage({ kind: 'snotes-pong', version: '1.0.0' }, '*');
|
|
break;
|
|
case 'snotes-settings': {
|
|
var data = {};
|
|
if (msg.serverUrl !== undefined) data.serverUrl = msg.serverUrl;
|
|
if (Object.keys(data).length > 0) browser.storage.sync.set(data);
|
|
window.postMessage({ kind: 'snotes-settings-ack', ok: true }, '*');
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
// ── Extension→Page messaging (capture context for the popup) ──────────────
|
|
browser.runtime.onMessage.addListener(function (msg) {
|
|
if (!msg || typeof msg.kind !== 'string') return Promise.resolve(undefined);
|
|
|
|
if (msg.kind === 'get_context') {
|
|
var selection = '';
|
|
try { selection = window.getSelection().toString().trim(); } catch (e) {}
|
|
return Promise.resolve({
|
|
selection: selection,
|
|
title: document.title || '',
|
|
url: window.location.href || '',
|
|
});
|
|
}
|
|
|
|
if (msg.kind === 'get_viewport') {
|
|
var ph = Math.max(
|
|
document.documentElement ? document.documentElement.scrollHeight : 0,
|
|
document.body ? document.body.scrollHeight : 0
|
|
);
|
|
return Promise.resolve({
|
|
viewportWidth: window.innerWidth || document.documentElement.clientWidth || 0,
|
|
viewportHeight: window.innerHeight || document.documentElement.clientHeight || 0,
|
|
pageHeight: ph,
|
|
devicePixelRatio: window.devicePixelRatio || 1,
|
|
scrollY: window.scrollY || window.pageYOffset || 0,
|
|
});
|
|
}
|
|
|
|
if (msg.kind === 'scroll_to') {
|
|
window.scrollTo(0, msg.y);
|
|
return Promise.resolve({ y: window.scrollY || window.pageYOffset || 0 });
|
|
}
|
|
|
|
return Promise.resolve(undefined);
|
|
});
|
|
})();
|