2018-05-16 15:16:14 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
if (window.jopext_hasRun) return;
|
|
|
|
window.jopext_hasRun = true;
|
|
|
|
|
|
|
|
console.info('jopext: Loading content script');
|
|
|
|
|
2018-05-24 19:32:30 +02:00
|
|
|
let browser_ = null;
|
|
|
|
if (typeof browser !== 'undefined') {
|
|
|
|
browser_ = browser;
|
|
|
|
browserSupportsPromises_ = true;
|
|
|
|
} else if (typeof chrome !== 'undefined') {
|
|
|
|
browser_ = chrome;
|
|
|
|
browserSupportsPromises_ = false;
|
|
|
|
}
|
|
|
|
|
2019-05-11 12:13:13 +02:00
|
|
|
function absoluteUrl(url) {
|
|
|
|
if (!url) return url;
|
|
|
|
const protocol = url.toLowerCase().split(':')[0];
|
|
|
|
if (['http', 'https', 'file'].indexOf(protocol) >= 0) return url;
|
|
|
|
|
2019-06-11 02:09:48 +02:00
|
|
|
if (url.indexOf('//') === 0) {
|
2019-05-11 12:13:13 +02:00
|
|
|
return location.protocol + url;
|
|
|
|
} else if (url[0] === '/') {
|
|
|
|
return location.protocol + '//' + location.host + url;
|
|
|
|
} else {
|
|
|
|
return baseUrl() + '/' + url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
function pageTitle() {
|
|
|
|
const titleElements = document.getElementsByTagName("title");
|
|
|
|
if (titleElements.length) return titleElements[0].text.trim();
|
|
|
|
return document.title.trim();
|
|
|
|
}
|
|
|
|
|
2019-06-11 02:09:48 +02:00
|
|
|
function pageLocationOrigin() {
|
|
|
|
// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)
|
|
|
|
// but for file:// protocol this is browser dependant and in particular Firefox returns "null"
|
|
|
|
// in this case.
|
|
|
|
|
|
|
|
if (location.protocol === 'file:') {
|
|
|
|
return 'file://';
|
|
|
|
} else {
|
|
|
|
return location.origin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
function baseUrl() {
|
2019-06-11 02:09:48 +02:00
|
|
|
let output = pageLocationOrigin() + location.pathname;
|
2018-05-16 15:16:14 +02:00
|
|
|
if (output[output.length - 1] !== '/') {
|
|
|
|
output = output.split('/');
|
|
|
|
output.pop();
|
|
|
|
output = output.join('/');
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2019-05-11 12:13:13 +02:00
|
|
|
function getImageSizes(element, forceAbsoluteUrls = false) {
|
2019-01-20 17:26:43 +02:00
|
|
|
const images = element.getElementsByTagName('img');
|
|
|
|
const output = {};
|
|
|
|
for (let i = 0; i < images.length; i++) {
|
|
|
|
const img = images[i];
|
2019-05-11 12:13:13 +02:00
|
|
|
const src = forceAbsoluteUrls ? absoluteUrl(img.src) : img.src;
|
|
|
|
output[src] = {
|
2019-01-20 17:26:43 +02:00
|
|
|
width: img.width,
|
|
|
|
height: img.height,
|
|
|
|
naturalWidth: img.naturalWidth,
|
|
|
|
naturalHeight: img.naturalHeight,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2019-06-13 01:26:09 +02:00
|
|
|
function getAnchorNames(element) {
|
|
|
|
const anchors = element.getElementsByTagName('a');
|
|
|
|
const output = [];
|
|
|
|
for (let i = 0; i < anchors.length; i++) {
|
|
|
|
const anchor = anchors[i];
|
|
|
|
if (anchor.id) {
|
|
|
|
output.push(anchor.id);
|
|
|
|
} else if (anchor.name) {
|
|
|
|
output.push(anchor.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
// Cleans up element by removing all its invisible children (which we don't want to render as Markdown)
|
2019-05-11 12:13:13 +02:00
|
|
|
function cleanUpElement(element, imageSizes) {
|
2018-05-16 15:16:14 +02:00
|
|
|
const childNodes = element.childNodes;
|
|
|
|
|
|
|
|
for (let i = 0; i < childNodes.length; i++) {
|
|
|
|
const node = childNodes[i];
|
2018-05-20 11:19:59 +02:00
|
|
|
|
|
|
|
let isVisible = node.nodeType === 1 ? window.getComputedStyle(node).display !== 'none' : true;
|
2018-06-29 20:45:33 +02:00
|
|
|
if (isVisible && ['input', 'textarea', 'script', 'noscript', 'style', 'select', 'option', 'button'].indexOf(node.nodeName.toLowerCase()) >= 0) isVisible = false;
|
2018-05-20 11:19:59 +02:00
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
if (!isVisible) {
|
|
|
|
element.removeChild(node);
|
|
|
|
} else {
|
2019-05-11 12:13:13 +02:00
|
|
|
|
|
|
|
if (node.nodeName.toLowerCase() === 'img') {
|
|
|
|
node.src = absoluteUrl(node.src);
|
|
|
|
const imageSize = imageSizes[node.src];
|
|
|
|
if (imageSize) {
|
|
|
|
node.width = imageSize.width;
|
|
|
|
node.height = imageSize.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanUpElement(node, imageSizes);
|
2018-05-16 15:16:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 00:41:52 +02:00
|
|
|
function documentForReadability() {
|
|
|
|
// Readability directly change the passed document so clone it so as
|
|
|
|
// to preserve the original web page.
|
|
|
|
return document.cloneNode(true);
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
function readabilityProcess() {
|
|
|
|
var uri = {
|
|
|
|
spec: location.href,
|
|
|
|
host: location.host,
|
|
|
|
prePath: location.protocol + "//" + location.host,
|
|
|
|
scheme: location.protocol.substr(0, location.protocol.indexOf(":")),
|
|
|
|
pathBase: location.protocol + "//" + location.host + location.pathname.substr(0, location.pathname.lastIndexOf("/") + 1)
|
|
|
|
};
|
|
|
|
|
2019-05-10 00:41:52 +02:00
|
|
|
const readability = new Readability(documentForReadability());
|
2018-05-16 15:16:14 +02:00
|
|
|
const article = readability.parse();
|
|
|
|
|
|
|
|
if (!article) throw new Error('Could not parse HTML document with Readability');
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: article.title,
|
|
|
|
body: article.content,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function prepareCommandResponse(command) {
|
2018-05-24 19:32:30 +02:00
|
|
|
console.info('Got command: ' + command.name);
|
|
|
|
|
2019-06-13 01:26:09 +02:00
|
|
|
const clippedContentResponse = (title, html, imageSizes, anchorNames) => {
|
2018-06-28 23:01:55 +02:00
|
|
|
return {
|
|
|
|
name: 'clippedContent',
|
|
|
|
title: title,
|
|
|
|
html: html,
|
|
|
|
base_url: baseUrl(),
|
2019-06-11 02:09:48 +02:00
|
|
|
url: pageLocationOrigin() + location.pathname + location.search,
|
2018-06-28 23:01:55 +02:00
|
|
|
parent_id: command.parent_id,
|
2018-09-23 19:03:11 +02:00
|
|
|
tags: command.tags || '',
|
2019-01-20 17:26:43 +02:00
|
|
|
image_sizes: imageSizes,
|
2019-06-13 01:26:09 +02:00
|
|
|
anchor_names: anchorNames,
|
2018-06-28 23:01:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
if (command.name === "simplifiedPageHtml") {
|
|
|
|
|
|
|
|
let article = null;
|
|
|
|
try {
|
|
|
|
article = readabilityProcess();
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error);
|
|
|
|
console.warn('Sending full page HTML instead');
|
|
|
|
const newCommand = Object.assign({}, command, { name: 'completePageHtml' });
|
|
|
|
const response = await prepareCommandResponse(newCommand);
|
|
|
|
response.warning = 'Could not retrieve simplified version of page - full page has been saved instead.';
|
|
|
|
return response;
|
|
|
|
}
|
2019-06-13 01:26:09 +02:00
|
|
|
return clippedContentResponse(article.title, article.body, getImageSizes(document), getAnchorNames(document));
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2019-05-10 00:41:52 +02:00
|
|
|
} else if (command.name === "isProbablyReaderable") {
|
|
|
|
|
|
|
|
const ok = isProbablyReaderable(documentForReadability());
|
|
|
|
console.info('isProbablyReaderable', ok);
|
|
|
|
return { name: 'isProbablyReaderable', value: ok };
|
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
} else if (command.name === "completePageHtml") {
|
|
|
|
|
|
|
|
const cleanDocument = document.body.cloneNode(true);
|
2019-05-11 12:13:13 +02:00
|
|
|
const imageSizes = getImageSizes(document, true);
|
|
|
|
cleanUpElement(cleanDocument, imageSizes);
|
2019-06-13 01:26:09 +02:00
|
|
|
return clippedContentResponse(pageTitle(), cleanDocument.innerHTML, imageSizes, getAnchorNames(document));
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-06-28 23:01:55 +02:00
|
|
|
} else if (command.name === "selectedHtml") {
|
|
|
|
|
|
|
|
const range = window.getSelection().getRangeAt(0);
|
|
|
|
const container = document.createElement('div');
|
|
|
|
container.appendChild(range.cloneContents());
|
2019-06-13 01:26:09 +02:00
|
|
|
return clippedContentResponse(pageTitle(), container.innerHTML, getImageSizes(document), getAnchorNames(document));
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-05-24 19:32:30 +02:00
|
|
|
} else if (command.name === 'screenshot') {
|
|
|
|
|
|
|
|
const overlay = document.createElement('div');
|
2018-05-26 16:53:50 +02:00
|
|
|
overlay.style.opacity = '0.6';
|
2018-05-24 19:32:30 +02:00
|
|
|
overlay.style.background = 'black';
|
|
|
|
overlay.style.width = '100%';
|
|
|
|
overlay.style.height = '100%';
|
|
|
|
overlay.style.zIndex = 99999999;
|
|
|
|
overlay.style.top = 0;
|
|
|
|
overlay.style.left = 0;
|
|
|
|
overlay.style.position = 'fixed';
|
|
|
|
|
|
|
|
document.body.appendChild(overlay);
|
|
|
|
|
2018-05-25 09:51:54 +02:00
|
|
|
const messageComp = document.createElement('div');
|
|
|
|
|
|
|
|
const messageCompWidth = 300;
|
|
|
|
messageComp.style.position = 'fixed'
|
2018-05-26 16:53:50 +02:00
|
|
|
messageComp.style.opacity = '0.95'
|
|
|
|
messageComp.style.fontSize = '14px';
|
2018-05-25 09:51:54 +02:00
|
|
|
messageComp.style.width = messageCompWidth + 'px'
|
|
|
|
messageComp.style.maxWidth = messageCompWidth + 'px'
|
|
|
|
messageComp.style.border = '1px solid black'
|
|
|
|
messageComp.style.background = 'white'
|
2018-09-29 13:53:16 +02:00
|
|
|
messageComp.style.color = 'black';
|
2018-05-25 09:51:54 +02:00
|
|
|
messageComp.style.top = '10px'
|
|
|
|
messageComp.style.textAlign = 'center';
|
2018-05-26 16:53:50 +02:00
|
|
|
messageComp.style.padding = '10px'
|
2018-05-25 09:51:54 +02:00
|
|
|
messageComp.style.left = Math.round(document.body.clientWidth / 2 - messageCompWidth / 2) + 'px'
|
|
|
|
messageComp.style.zIndex = overlay.style.zIndex + 1
|
|
|
|
|
|
|
|
messageComp.textContent = 'Drag and release to capture a screenshot';
|
|
|
|
|
|
|
|
document.body.appendChild(messageComp);
|
|
|
|
|
2018-05-24 19:32:30 +02:00
|
|
|
const selection = document.createElement('div');
|
2018-05-25 09:51:54 +02:00
|
|
|
selection.style.opacity = '0.4';
|
|
|
|
selection.style.border = '1px solid red';
|
|
|
|
selection.style.background = 'white';
|
|
|
|
selection.style.border = '2px solid black';
|
2018-05-24 19:32:30 +02:00
|
|
|
selection.style.zIndex = overlay.style.zIndex - 1;
|
|
|
|
selection.style.top = 0;
|
|
|
|
selection.style.left = 0;
|
|
|
|
selection.style.position = 'fixed';
|
|
|
|
|
|
|
|
document.body.appendChild(selection);
|
|
|
|
|
|
|
|
let isDragging = false;
|
|
|
|
let draggingStartPos = null;
|
|
|
|
let selectionArea = {};
|
|
|
|
|
|
|
|
function updateSelection() {
|
2018-05-25 09:51:54 +02:00
|
|
|
selection.style.left = selectionArea.x + 'px';
|
|
|
|
selection.style.top = selectionArea.y + 'px';
|
|
|
|
selection.style.width = selectionArea.width + 'px';
|
|
|
|
selection.style.height = selectionArea.height + 'px';
|
2018-05-24 19:32:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setSelectionSizeFromMouse(event) {
|
2018-05-25 09:51:54 +02:00
|
|
|
selectionArea.width = Math.max(1, event.clientX - draggingStartPos.x);
|
|
|
|
selectionArea.height = Math.max(1, event.clientY - draggingStartPos.y);
|
2018-05-24 19:32:30 +02:00
|
|
|
updateSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
function selection_mouseDown(event) {
|
2018-05-25 09:51:54 +02:00
|
|
|
selectionArea = { x: event.clientX, y: event.clientY, width: 0, height: 0 }
|
|
|
|
draggingStartPos = { x: event.clientX, y: event.clientY };
|
2018-05-24 19:32:30 +02:00
|
|
|
isDragging = true;
|
|
|
|
updateSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
function selection_mouseMove(event) {
|
|
|
|
if (!isDragging) return;
|
|
|
|
setSelectionSizeFromMouse(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
function selection_mouseUp(event) {
|
|
|
|
setSelectionSizeFromMouse(event);
|
|
|
|
|
|
|
|
isDragging = false;
|
|
|
|
|
|
|
|
overlay.removeEventListener('mousedown', selection_mouseDown);
|
|
|
|
overlay.removeEventListener('mousemove', selection_mouseMove);
|
|
|
|
overlay.removeEventListener('mouseup', selection_mouseUp);
|
|
|
|
|
|
|
|
document.body.removeChild(overlay);
|
|
|
|
document.body.removeChild(selection);
|
2018-05-25 09:51:54 +02:00
|
|
|
document.body.removeChild(messageComp);
|
|
|
|
|
2018-06-01 17:12:49 +02:00
|
|
|
console.info('jopext: selectionArea:', selectionArea);
|
|
|
|
|
2018-05-25 09:51:54 +02:00
|
|
|
if (!selectionArea || !selectionArea.width || !selectionArea.height) return;
|
|
|
|
|
2018-05-26 16:53:50 +02:00
|
|
|
// Need to wait a bit before taking the screenshot to make sure
|
|
|
|
// the overlays have been removed and don't appear in the
|
|
|
|
// screenshot. 10ms is not enough.
|
2018-05-25 09:51:54 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
const content = {
|
|
|
|
title: pageTitle(),
|
2018-06-01 16:50:11 +02:00
|
|
|
crop_rect: selectionArea,
|
2019-06-11 02:09:48 +02:00
|
|
|
url: pageLocationOrigin() + location.pathname,
|
2018-06-01 16:50:11 +02:00
|
|
|
parent_id: command.parent_id,
|
2018-09-23 19:03:11 +02:00
|
|
|
tags: command.tags,
|
2018-05-25 09:51:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
browser_.runtime.sendMessage({
|
|
|
|
name: 'screenshotArea',
|
|
|
|
content: content,
|
2018-06-01 16:50:11 +02:00
|
|
|
api_base_url: command.api_base_url,
|
2018-05-25 09:51:54 +02:00
|
|
|
});
|
2018-05-26 16:53:50 +02:00
|
|
|
}, 100);
|
2018-05-24 19:32:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
overlay.addEventListener('mousedown', selection_mouseDown);
|
|
|
|
overlay.addEventListener('mousemove', selection_mouseMove);
|
|
|
|
overlay.addEventListener('mouseup', selection_mouseUp);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
2019-02-15 01:05:28 +02:00
|
|
|
} else if (command.name === "pageUrl") {
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-06-11 02:09:48 +02:00
|
|
|
let url = pageLocationOrigin() + location.pathname + location.search;
|
2019-06-13 01:26:09 +02:00
|
|
|
return clippedContentResponse(pageTitle(), url, getImageSizes(document), getAnchorNames(document));
|
2019-02-15 01:05:28 +02:00
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
} else {
|
2018-05-24 19:32:30 +02:00
|
|
|
throw new Error('Unknown command: ' + JSON.stringify(command));
|
2018-05-16 15:16:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function execCommand(command) {
|
|
|
|
const response = await prepareCommandResponse(command);
|
2018-05-24 19:32:30 +02:00
|
|
|
browser_.runtime.sendMessage(response);
|
2018-05-16 15:16:14 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 19:32:30 +02:00
|
|
|
browser_.runtime.onMessage.addListener((command) => {
|
2018-05-16 15:16:14 +02:00
|
|
|
console.info('jopext: Got command:', command);
|
|
|
|
|
|
|
|
execCommand(command);
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|