From d6218f35fe8f47c82d77ae3ce5ff2a0efb1cb2d5 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 11 Jun 2019 01:09:48 +0100 Subject: [PATCH] Clipper: Fixes #1526: Local files can be clipped again --- Clipper/joplin-webclipper/background.js | 9 ++++---- .../content_scripts/index.js | 22 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Clipper/joplin-webclipper/background.js b/Clipper/joplin-webclipper/background.js index f178b102d8..f9fea65e21 100644 --- a/Clipper/joplin-webclipper/background.js +++ b/Clipper/joplin-webclipper/background.js @@ -23,11 +23,12 @@ window.joplinEnv = function() { return env_; } -async function browserCaptureVisibleTabs(windowId, options) { - if (browserSupportsPromises_) return browser_.tabs.captureVisibleTab(windowId, { format: 'jpeg' }); +async function browserCaptureVisibleTabs(windowId) { + const options = { format: 'jpeg' }; + if (browserSupportsPromises_) return browser_.tabs.captureVisibleTab(windowId, options); return new Promise((resolve, reject) => { - browser_.tabs.captureVisibleTab(windowId, { format: 'jpeg' }, (image) => { + browser_.tabs.captureVisibleTab(windowId, options, (image) => { resolve(image); }); }); @@ -56,7 +57,7 @@ browser_.runtime.onMessage.addListener(async (command) => { const zoom = await browserGetZoom(); - const imageDataUrl = await browserCaptureVisibleTabs(null, { format: 'jpeg' }); + const imageDataUrl = await browserCaptureVisibleTabs(null); content = Object.assign({}, command.content); content.image_data_url = imageDataUrl; diff --git a/Clipper/joplin-webclipper/content_scripts/index.js b/Clipper/joplin-webclipper/content_scripts/index.js index cf9774c634..67c8af6031 100644 --- a/Clipper/joplin-webclipper/content_scripts/index.js +++ b/Clipper/joplin-webclipper/content_scripts/index.js @@ -19,7 +19,7 @@ const protocol = url.toLowerCase().split(':')[0]; if (['http', 'https', 'file'].indexOf(protocol) >= 0) return url; - if (url.indexOf('//')) { + if (url.indexOf('//') === 0) { return location.protocol + url; } else if (url[0] === '/') { return location.protocol + '//' + location.host + url; @@ -34,8 +34,20 @@ return document.title.trim(); } + 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; + } + } + function baseUrl() { - let output = location.origin + location.pathname; + let output = pageLocationOrigin() + location.pathname; if (output[output.length - 1] !== '/') { output = output.split('/'); output.pop(); @@ -123,7 +135,7 @@ title: title, html: html, base_url: baseUrl(), - url: location.origin + location.pathname + location.search, + url: pageLocationOrigin() + location.pathname + location.search, parent_id: command.parent_id, tags: command.tags || '', image_sizes: imageSizes, @@ -265,7 +277,7 @@ const content = { title: pageTitle(), crop_rect: selectionArea, - url: location.origin + location.pathname, + url: pageLocationOrigin() + location.pathname, parent_id: command.parent_id, tags: command.tags, }; @@ -286,7 +298,7 @@ } else if (command.name === "pageUrl") { - let url = location.origin + location.pathname + location.search; + let url = pageLocationOrigin() + location.pathname + location.search; return clippedContentResponse(pageTitle(), url, getImageSizes(document)); } else {