2024-03-14 20:38:20 +02:00
|
|
|
import joplinEnv from './util/joplinEnv.mjs';
|
|
|
|
import getActiveTabs from './util/getActiveTabs.mjs';
|
|
|
|
|
2018-05-24 19:32:30 +02:00
|
|
|
let browser_ = null;
|
|
|
|
if (typeof browser !== 'undefined') {
|
|
|
|
browser_ = browser;
|
|
|
|
} else if (typeof chrome !== 'undefined') {
|
|
|
|
browser_ = chrome;
|
|
|
|
}
|
|
|
|
|
2019-06-11 02:09:48 +02:00
|
|
|
async function browserCaptureVisibleTabs(windowId) {
|
2020-11-16 01:21:46 +02:00
|
|
|
const options = {
|
|
|
|
format: 'jpeg',
|
|
|
|
|
|
|
|
// This is supposed to be the default quality, but in fact Firefox 82+
|
|
|
|
// clearly uses a much lower quality, closer to 20 or 30, so we have to
|
2024-02-26 12:16:23 +02:00
|
|
|
// set it here explicitly.
|
2020-11-16 01:21:46 +02:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/extensionTypes/ImageDetails
|
|
|
|
// https://discourse.joplinapp.org/t/clip-screenshot-image-quality/12302/4
|
|
|
|
quality: 92,
|
|
|
|
};
|
2024-03-14 20:38:20 +02:00
|
|
|
return browser_.tabs.captureVisibleTab(windowId, options);
|
2018-06-01 17:12:49 +02:00
|
|
|
}
|
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
browser_.runtime.onInstalled.addListener(() => {
|
2024-03-14 20:38:20 +02:00
|
|
|
if (joplinEnv() === 'dev') {
|
|
|
|
browser_.action.setIcon({
|
2018-05-26 12:18:54 +02:00
|
|
|
path: 'icons/32-dev.png',
|
|
|
|
});
|
|
|
|
}
|
2018-05-24 19:32:30 +02:00
|
|
|
});
|
|
|
|
|
2020-11-16 00:30:29 +02:00
|
|
|
browser_.runtime.onMessage.addListener(async (command) => {
|
|
|
|
if (command.name === 'screenshotArea') {
|
|
|
|
// The dimensions of the image returned by Firefox are the regular ones,
|
|
|
|
// while the one returned by Chrome depend on the screen pixel ratio. So
|
|
|
|
// it would return a 600*400 image if the window dimensions are 300x200
|
|
|
|
// and the screen pixel ratio is 2.
|
|
|
|
//
|
|
|
|
// Normally we could rely on window.devicePixelRatio to tell us that but
|
|
|
|
// since Firefox and Chrome handle this differently, we need to
|
|
|
|
// calculate the ratio ourselves. It's simply the image dimensions
|
|
|
|
// divided by the window inner width.
|
|
|
|
//
|
|
|
|
// The crop rectangle is always in real pixels, so we need to multiply
|
|
|
|
// it by the ratio we've calculated.
|
2024-03-14 20:38:20 +02:00
|
|
|
//
|
|
|
|
// 8/3/2024: With manifest v3, we don't have access to DOM APIs in Chrome.
|
|
|
|
// As a result, we can't easily calculate the size of the captured image.
|
|
|
|
// We instead base the crop region exclusively on window.devicePixelRatio,
|
|
|
|
// which seems to work in modern Firefox and Chrome.
|
2019-06-11 02:09:48 +02:00
|
|
|
const imageDataUrl = await browserCaptureVisibleTabs(null);
|
2020-11-16 00:30:29 +02:00
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
const content = { ...command.content };
|
2018-06-01 17:12:49 +02:00
|
|
|
content.image_data_url = imageDataUrl;
|
2019-11-07 20:12:14 +02:00
|
|
|
if ('url' in content) content.source_url = content.url;
|
2018-06-01 17:12:49 +02:00
|
|
|
|
2024-03-14 20:38:20 +02:00
|
|
|
const ratio = content.devicePixelRatio;
|
2023-06-01 13:02:36 +02:00
|
|
|
const newArea = { ...command.content.crop_rect };
|
2020-11-16 00:30:29 +02:00
|
|
|
newArea.x *= ratio;
|
|
|
|
newArea.y *= ratio;
|
|
|
|
newArea.width *= ratio;
|
|
|
|
newArea.height *= ratio;
|
2018-06-01 17:12:49 +02:00
|
|
|
content.crop_rect = newArea;
|
|
|
|
|
2021-06-22 20:57:04 +02:00
|
|
|
fetch(`${command.api_base_url}/notes?token=${encodeURIComponent(command.token)}`, {
|
2019-07-30 09:35:42 +02:00
|
|
|
method: 'POST',
|
2018-06-01 17:12:49 +02:00
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
2019-07-30 09:35:42 +02:00
|
|
|
'Content-Type': 'application/json',
|
2018-06-01 17:12:49 +02:00
|
|
|
},
|
2019-07-30 09:35:42 +02:00
|
|
|
body: JSON.stringify(content),
|
2018-05-24 19:32:30 +02:00
|
|
|
});
|
|
|
|
}
|
2018-05-26 12:18:54 +02:00
|
|
|
});
|
2020-02-11 12:49:07 +02:00
|
|
|
|
|
|
|
async function sendClipMessage(clipType) {
|
2024-03-14 20:38:20 +02:00
|
|
|
const tabs = await getActiveTabs(browser_);
|
2020-02-11 12:49:07 +02:00
|
|
|
if (!tabs || !tabs.length) {
|
|
|
|
console.error('No active tabs');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const tabId = tabs[0].id;
|
|
|
|
// send a message to the content script on the active tab (assuming it's there)
|
|
|
|
const message = {
|
|
|
|
shouldSendToJoplin: true,
|
|
|
|
};
|
|
|
|
switch (clipType) {
|
|
|
|
case 'clipCompletePage':
|
|
|
|
message.name = 'completePageHtml';
|
|
|
|
message.preProcessFor = 'markdown';
|
|
|
|
break;
|
|
|
|
case 'clipCompletePageHtml':
|
|
|
|
message.name = 'completePageHtml';
|
|
|
|
message.preProcessFor = 'html';
|
|
|
|
break;
|
|
|
|
case 'clipSimplifiedPage':
|
|
|
|
message.name = 'simplifiedPageHtml';
|
|
|
|
break;
|
|
|
|
case 'clipUrl':
|
|
|
|
message.name = 'pageUrl';
|
|
|
|
break;
|
|
|
|
case 'clipSelection':
|
|
|
|
message.name = 'selectedHtml';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (message.name) {
|
|
|
|
browser_.tabs.sendMessage(tabId, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
browser_.commands.onCommand.addListener((command) => {
|
2020-02-11 12:49:07 +02:00
|
|
|
// We could enumerate these twice, but since we're in here first,
|
|
|
|
// why not save ourselves the trouble with this convention
|
|
|
|
if (command.startsWith('clip')) {
|
|
|
|
sendClipMessage(command);
|
|
|
|
}
|
|
|
|
});
|