From 0eabd5cd9afaaca1ed0adb662dae5d948b2e971a Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 02:02:20 +0900 Subject: [PATCH 01/11] =?UTF-8?q?manifest.json=E3=82=92v3=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/manifest.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index 526048d..2df1f6d 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,11 +1,10 @@ { "name": "__MSG_extName__", - "version": "1.0.2", + "version": "2.0.0", "description": "__MSG_extDescription__", "permissions": ["contextMenus", "clipboardWrite"], "background": { - "scripts": ["background.js"], - "persistent": false + "service_worker": ["background.js"] }, "default_locale": "en", "icons": { @@ -14,5 +13,5 @@ "48": "images/ImgSrc-er48.png", "128": "images/ImgSrc-er128.png" }, - "manifest_version": 2 + "manifest_version": 3 } From 1f1e7dfe5562e893d477fd3825ad04ccd0930cf5 Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 13:04:56 +0900 Subject: [PATCH 02/11] Change data type of background.service_worker from array to string --- src/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manifest.json b/src/manifest.json index 2df1f6d..7db93a2 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -4,7 +4,7 @@ "description": "__MSG_extDescription__", "permissions": ["contextMenus", "clipboardWrite"], "background": { - "service_worker": ["background.js"] + "service_worker": "background.js" }, "default_locale": "en", "icons": { From 3360b02f83207f4c17402676fab0dc130ac94d0d Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 13:16:15 +0900 Subject: [PATCH 03/11] Replace window.alert with console.error --- src/background.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/background.js b/src/background.js index 77fcc6f..91b08d2 100644 --- a/src/background.js +++ b/src/background.js @@ -50,9 +50,8 @@ function convertToImgTag(info) { textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); - } catch (error) { - let message = `${error.message}\n\n▼詳細\n${error.stack}`; - alert(message); + } catch (e) { + console.error(e.stack); } } From 151e2d98c7fa832a22bcb91c95dcc0da71e75394 Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 02:02:20 +0900 Subject: [PATCH 04/11] =?UTF-8?q?manifest.json=E3=82=92v3=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/manifest.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index 526048d..2df1f6d 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,11 +1,10 @@ { "name": "__MSG_extName__", - "version": "1.0.2", + "version": "2.0.0", "description": "__MSG_extDescription__", "permissions": ["contextMenus", "clipboardWrite"], "background": { - "scripts": ["background.js"], - "persistent": false + "service_worker": ["background.js"] }, "default_locale": "en", "icons": { @@ -14,5 +13,5 @@ "48": "images/ImgSrc-er48.png", "128": "images/ImgSrc-er128.png" }, - "manifest_version": 2 + "manifest_version": 3 } From f6094d9d11b93a01d6a965c83f630838c3c2129e Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 13:04:56 +0900 Subject: [PATCH 05/11] Change data type of background.service_worker from array to string --- src/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manifest.json b/src/manifest.json index 2df1f6d..7db93a2 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -4,7 +4,7 @@ "description": "__MSG_extDescription__", "permissions": ["contextMenus", "clipboardWrite"], "background": { - "service_worker": ["background.js"] + "service_worker": "background.js" }, "default_locale": "en", "icons": { From d86bd32cb2ffe2dacd2d265fffd0cc04f9c9dbfb Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 13:16:15 +0900 Subject: [PATCH 06/11] Replace window.alert with console.error --- src/background.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/background.js b/src/background.js index 77fcc6f..91b08d2 100644 --- a/src/background.js +++ b/src/background.js @@ -50,9 +50,8 @@ function convertToImgTag(info) { textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); - } catch (error) { - let message = `${error.message}\n\n▼詳細\n${error.stack}`; - alert(message); + } catch (e) { + console.error(e.stack); } } From c831a05c0605c89d2ec79e9fb0b5c1f87ec634ab Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 02:02:20 +0900 Subject: [PATCH 07/11] =?UTF-8?q?manifest.json=E3=82=92v3=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/manifest.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index 526048d..2df1f6d 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,11 +1,10 @@ { "name": "__MSG_extName__", - "version": "1.0.2", + "version": "2.0.0", "description": "__MSG_extDescription__", "permissions": ["contextMenus", "clipboardWrite"], "background": { - "scripts": ["background.js"], - "persistent": false + "service_worker": ["background.js"] }, "default_locale": "en", "icons": { @@ -14,5 +13,5 @@ "48": "images/ImgSrc-er48.png", "128": "images/ImgSrc-er128.png" }, - "manifest_version": 2 + "manifest_version": 3 } From b3391a04bb3a308989c5cdd04aacbf58916e8bc1 Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 13:04:56 +0900 Subject: [PATCH 08/11] Change data type of background.service_worker from array to string --- src/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manifest.json b/src/manifest.json index 2df1f6d..7db93a2 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -4,7 +4,7 @@ "description": "__MSG_extDescription__", "permissions": ["contextMenus", "clipboardWrite"], "background": { - "service_worker": ["background.js"] + "service_worker": "background.js" }, "default_locale": "en", "icons": { From 5a5b4134c7bf05e246685f1d3c698258defa2cfb Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 18 Aug 2021 13:16:15 +0900 Subject: [PATCH 09/11] Replace window.alert with console.error --- src/background.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/background.js b/src/background.js index 77fcc6f..91b08d2 100644 --- a/src/background.js +++ b/src/background.js @@ -50,9 +50,8 @@ function convertToImgTag(info) { textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); - } catch (error) { - let message = `${error.message}\n\n▼詳細\n${error.stack}`; - alert(message); + } catch (e) { + console.error(e.stack); } } From 7a16073193a2cca88bc11952fec7e80931f5d1d6 Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Wed, 4 May 2022 14:25:28 +0900 Subject: [PATCH 10/11] Add content script for copying text to clipboard --- src/background.js | 13 ++++++++++++- src/content.js | 36 ++++++++++++++++++++++++++++++++++++ src/manifest.json | 6 ++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/content.js diff --git a/src/background.js b/src/background.js index 91b08d2..2244126 100644 --- a/src/background.js +++ b/src/background.js @@ -44,12 +44,23 @@ function convertToImgTag(info) { taggedUrl = imgTaggedUrl(info.srcUrl); } // Save to clipboard + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { + chrome.tabs.sendMessage( + tabs[0].id, + { + message: 'copyText', + textToCopy: taggedUrl, + }, + (response) => console.info(response) + ); + }); + /* let textArea = document.createElement('textarea'); document.body.appendChild(textArea); textArea.value = taggedUrl; textArea.select(); document.execCommand('copy'); - document.body.removeChild(textArea); + document.body.removeChild(textArea);*/ } catch (e) { console.error(e.stack); } diff --git a/src/content.js b/src/content.js new file mode 100644 index 0000000..87cb88e --- /dev/null +++ b/src/content.js @@ -0,0 +1,36 @@ +// Copyright 2022 Taro TSUKAGOSHI +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* global chrome */ + +chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { + if (request.message === 'copyText') { + console.log( + `sender: ${JSON.stringify(sender)}, sendResponse: ${sendResponse}` + ); + copyToTheClipboard(request.textToCopy); + } +}); + +function copyToTheClipboard(textToCopy) { + const el = document.createElement('textarea'); + el.value = textToCopy; + el.setAttribute('readonly', ''); + el.style.position = 'absolute'; + el.style.left = '-9999px'; + document.body.appendChild(el); + el.select(); + document.execCommand('copy'); + document.body.removeChild(el); +} diff --git a/src/manifest.json b/src/manifest.json index 7db93a2..284baf5 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -6,6 +6,12 @@ "background": { "service_worker": "background.js" }, + "content_scripts": [ + { + "matches": [""], + "js": ["content.js"] + } + ], "default_locale": "en", "icons": { "16": "images/ImgSrc-er16.png", From 48b22e59d1c97f717e754aea98ac07b28febeef0 Mon Sep 17 00:00:00 2001 From: ttsukagoshi Date: Thu, 5 May 2022 00:07:47 +0900 Subject: [PATCH 11/11] Refactoring --- src/background.js | 39 ++++++++------------------------------- src/content.js | 10 +++++++--- 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/background.js b/src/background.js index 2244126..7b7deeb 100644 --- a/src/background.js +++ b/src/background.js @@ -23,19 +23,9 @@ chrome.runtime.onInstalled.addListener(function () { }); }); -chrome.contextMenus.onClicked.addListener(function (info) { +chrome.contextMenus.onClicked.addListener((info) => { if (info.menuItemId == 'ImgSrc-er') { - convertToImgTag(info); - } -}); - -/** - * Convert the selected object into an HTML img tag and save the string on clipboard. - * @param {Object} info Object containing the information on the right-clicked object. - */ -function convertToImgTag(info) { - var taggedUrl = ''; - try { + let taggedUrl = ''; if (info.selectionText) { taggedUrl = imgTaggedUrl(info.selectionText); } else if (info.linkUrl) { @@ -44,27 +34,14 @@ function convertToImgTag(info) { taggedUrl = imgTaggedUrl(info.srcUrl); } // Save to clipboard - chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { - chrome.tabs.sendMessage( - tabs[0].id, - { - message: 'copyText', - textToCopy: taggedUrl, - }, - (response) => console.info(response) - ); + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, { + message: 'copyText', + textToCopy: taggedUrl, + }); }); - /* - let textArea = document.createElement('textarea'); - document.body.appendChild(textArea); - textArea.value = taggedUrl; - textArea.select(); - document.execCommand('copy'); - document.body.removeChild(textArea);*/ - } catch (e) { - console.error(e.stack); } -} +}); /** * Encapsulate the entered URL string in an HTML img tag. diff --git a/src/content.js b/src/content.js index 87cb88e..6f3acbc 100644 --- a/src/content.js +++ b/src/content.js @@ -14,12 +14,16 @@ /* global chrome */ -chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.message === 'copyText') { - console.log( - `sender: ${JSON.stringify(sender)}, sendResponse: ${sendResponse}` + console.info( + `[ImgSrc-er] Copying text "${ + request.textToCopy + }" to clipboard (sender: ${JSON.stringify(sender)})` ); copyToTheClipboard(request.textToCopy); + sendResponse(); + return; } });