diff --git a/src/background.js b/src/background.js index 91b08d2..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,16 +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 (e) { - console.error(e.stack); + 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 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",