2018-05-24 19:32:30 +02:00
|
|
|
let browser_ = null;
|
2018-06-01 16:50:11 +02:00
|
|
|
let browserName_ = null;
|
2018-05-24 19:32:30 +02:00
|
|
|
if (typeof browser !== 'undefined') {
|
|
|
|
browser_ = browser;
|
|
|
|
browserSupportsPromises_ = true;
|
2018-06-01 16:50:11 +02:00
|
|
|
browserName_ = 'firefox';
|
2018-05-24 19:32:30 +02:00
|
|
|
} else if (typeof chrome !== 'undefined') {
|
|
|
|
browser_ = chrome;
|
|
|
|
browserSupportsPromises_ = false;
|
2018-06-01 16:50:11 +02:00
|
|
|
browserName_ = 'chrome';
|
2018-05-24 19:32:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 16:50:11 +02:00
|
|
|
let env_ = null;
|
|
|
|
|
|
|
|
// Make this function global so that it can be accessed
|
|
|
|
// from the popup too.
|
|
|
|
// https://stackoverflow.com/questions/6323184/communication-between-background-page-and-popup-page-in-a-chrome-extension
|
|
|
|
window.joplinEnv = function() {
|
|
|
|
if (env_) return env_;
|
|
|
|
|
2018-06-14 20:11:22 +02:00
|
|
|
const manifest = browser_.runtime.getManifest();
|
|
|
|
env_ = manifest.name.indexOf('[DEV]') >= 0 ? 'dev' : 'prod';
|
2018-06-01 16:50:11 +02:00
|
|
|
return env_;
|
2018-05-26 12:18:54 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 19:32:30 +02:00
|
|
|
async function browserCaptureVisibleTabs(windowId, options) {
|
2018-05-25 09:51:54 +02:00
|
|
|
if (browserSupportsPromises_) return browser_.tabs.captureVisibleTab(windowId, { format: 'jpeg' });
|
2018-05-24 19:32:30 +02:00
|
|
|
|
2018-05-25 09:51:54 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
browser_.tabs.captureVisibleTab(windowId, { format: 'jpeg' }, (image) => {
|
2018-05-24 19:32:30 +02:00
|
|
|
resolve(image);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:12:49 +02:00
|
|
|
async function browserGetZoom(tabId) {
|
|
|
|
if (browserSupportsPromises_) return browser_.tabs.getZoom(tabId);
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
browser_.tabs.getZoom(tabId, (zoom) => {
|
|
|
|
resolve(zoom);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:50:11 +02:00
|
|
|
browser_.runtime.onInstalled.addListener(function(details) {
|
|
|
|
if (window.joplinEnv() === 'dev') {
|
2018-05-26 12:18:54 +02:00
|
|
|
browser_.browserAction.setIcon({
|
|
|
|
path: 'icons/32-dev.png',
|
|
|
|
});
|
|
|
|
}
|
2018-05-24 19:32:30 +02:00
|
|
|
});
|
|
|
|
|
2018-06-01 17:12:49 +02:00
|
|
|
browser_.runtime.onMessage.addListener(async (command) => {
|
2018-05-24 19:32:30 +02:00
|
|
|
if (command.name === 'screenshotArea') {
|
|
|
|
|
2018-06-01 17:12:49 +02:00
|
|
|
const zoom = await browserGetZoom();
|
|
|
|
|
|
|
|
const imageDataUrl = await browserCaptureVisibleTabs(null, { format: 'jpeg' });
|
|
|
|
content = Object.assign({}, command.content);
|
|
|
|
content.image_data_url = imageDataUrl;
|
|
|
|
|
|
|
|
const newArea = Object.assign({}, command.content.crop_rect);
|
|
|
|
newArea.x *= zoom;
|
|
|
|
newArea.y *= zoom;
|
|
|
|
newArea.width *= zoom;
|
|
|
|
newArea.height *= zoom;
|
|
|
|
content.crop_rect = newArea;
|
|
|
|
|
|
|
|
fetch(command.api_base_url + "/notes", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(content)
|
2018-05-24 19:32:30 +02:00
|
|
|
});
|
|
|
|
}
|
2018-05-26 12:18:54 +02:00
|
|
|
});
|