diff --git a/src/background.js b/src/background.js index 77fcc6f..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,17 +34,14 @@ function convertToImgTag(info) { taggedUrl = imgTaggedUrl(info.srcUrl); } // Save to clipboard - let textArea = document.createElement('textarea'); - document.body.appendChild(textArea); - textArea.value = taggedUrl; - textArea.select(); - document.execCommand('copy'); - document.body.removeChild(textArea); - } catch (error) { - let message = `${error.message}\n\n▼詳細\n${error.stack}`; - alert(message); + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, { + message: 'copyText', + textToCopy: taggedUrl, + }); + }); } -} +}); /** * Encapsulate the entered URL string in an HTML img tag. diff --git a/src/content.js b/src/content.js new file mode 100644 index 0000000..6f3acbc --- /dev/null +++ b/src/content.js @@ -0,0 +1,40 @@ +// 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((request, sender, sendResponse) => { + if (request.message === 'copyText') { + console.info( + `[ImgSrc-er] Copying text "${ + request.textToCopy + }" to clipboard (sender: ${JSON.stringify(sender)})` + ); + copyToTheClipboard(request.textToCopy); + sendResponse(); + return; + } +}); + +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 526048d..284baf5 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,12 +1,17 @@ { "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" }, + "content_scripts": [ + { + "matches": [""], + "js": ["content.js"] + } + ], "default_locale": "en", "icons": { "16": "images/ImgSrc-er16.png", @@ -14,5 +19,5 @@ "48": "images/ImgSrc-er48.png", "128": "images/ImgSrc-er128.png" }, - "manifest_version": 2 + "manifest_version": 3 }