chrome插件:Click to copy the current tab‘s title and URL to the clipboard.

创建一个Chrome插件来复制当前标签页的标题和网址到剪贴板,你需要编写几个文件,包括一个manifest文件(插件的配置文件),一个背景脚本(background script),以及一个可选的弹出页面(popup)。以下是一个简单的示例,展示了如何实现这个功能:

  1. manifest.json - 插件的配置文件
{
  "name": "Offscreen API - Clipboard",
  "version": "1.0",
  "manifest_version": 3,
  "description": "Shows how to write a string to the system clipboard using the offscreen document.",
  "background": {
    "service_worker": "background.js"
  },
  "action": {    "default_icon": "icon.png"
	},
  "permissions": ["offscreen", "clipboardWrite", "activeTab", "tabs"]
}

  1. background.js - 背景脚本,处理点击事件
// Copyright 2023 Google LLC
//
// 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
//
//     https://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.

// The value that will be written to the clipboard.
let textToCopy = `Hello world!`;

// When the browser action is clicked, `addToClipboard()` will use an offscreen
// document to write the value of `textToCopy` to the system clipboard.
chrome.action.onClicked.addListener(async () => {


  const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
  const title = activeTab.title;
  const url = activeTab.url;
  // const textToCopy = title + '\n' + url;
   textToCopy = title + ': ' + url;

	
  await addToClipboard(textToCopy);
});

// Solution 1 - As of Jan 2023, service workers cannot directly interact with
// the system clipboard using either `navigator.clipboard` or
// `document.execCommand()`. To work around this, we'll create an offscreen
// document and pass it the data we want to write to the clipboard.
async function addToClipboard(value) {
  await chrome.offscreen.createDocument({
    url: 'offscreen.html',
    reasons: [chrome.offscreen.Reason.CLIPBOARD],
    justification: 'Write text to the clipboard.'
  });

  // Now that we have an offscreen document, we can dispatch the
  // message.
  chrome.runtime.sendMessage({
    type: 'copy-data-to-clipboard',
    target: 'offscreen-doc',
    data: value
  });
}

// Solution 2 – Once extension service workers can use the Clipboard API,
// replace the offscreen document based implementation with something like this.
// eslint-disable-next-line no-unused-vars -- This is an alternative implementation
async function addToClipboardV2(value) {
  navigator.clipboard.writeText(value);
}

  1. popup.html - 弹出页面(可选)
<!DOCTYPE html>
<html>
  <head>
    <title>Copy Tab Info</title>
    <style>
      body {
        width: 200px;
        height: 100px;
        margin: 15px;
        font-family: Arial, sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>Copy Tab Info</h1>
    <p>Click the extension icon to copy the current tab's title and URL to clipboard.</p>
  </body>
</html>
  1. icons - 图标文件(你需要准备这些图标文件)
  • icon16.png
  • icon48.png
  • icon128.png

将这些文件放在一个文件夹中,然后使用Chrome浏览器的扩展程序页面(chrome://extensions/)加载这个文件夹作为未打包的扩展程序。确保开发者模式已启用。

请注意,这个示例假设你已经有了图标文件,并且你的Chrome浏览器允许访问剪贴板。此外,由于Chrome的安全策略,某些权限可能需要在用户交互的上下文中才能使用。因此,使用browserAction的点击事件来触发复制操作是一个好方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值