这是indexloc提供的服务,不要输入任何密码
Skip to content

Switch to Manifest v3 (#7) #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions src/content.js
Original file line number Diff line number Diff line change
@@ -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);
}
13 changes: 9 additions & 4 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
{
"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": ["<all_urls>"],
"js": ["content.js"]
}
],
"default_locale": "en",
"icons": {
"16": "images/ImgSrc-er16.png",
"32": "images/ImgSrc-er32.png",
"48": "images/ImgSrc-er48.png",
"128": "images/ImgSrc-er128.png"
},
"manifest_version": 2
"manifest_version": 3
}