浏览器插件开发初体验(URL OPENER)

https://github.com/sxei/chrome-plugin-demo 参考资料

需求:

1、对于一串文本,提取其中的urls并打开这些url。

2、提取浏览器所有tabs的url和title

例如:

go 复制代码
// https://www.cnblogs.com/kelelipeng/p/17129941.html
// https://www.ruanyifeng.com/blog/2019/09/curl-reference.html
// https://ruanyifeng.com/blog/2011/09/curl.html
// Curl命令的data, data-ascii, data-binary, data-raw和data-urlencode选项详解  https://blog.csdn.net/taiyangdao/article/details/77020762
//-d可以指定json格式数据   https://www.cnblogs.com/kelelipeng/p/17129941.html

项目结构

选中项目文件夹即可。

popup.js

go 复制代码
document.getElementById('open-urls').onclick = function() {
  const userInput = document.getElementById('user-input').value;
  // 使用正则表达式提取URL
  const regex = /(https?:\/\/[^\s]+)/g;
  const urls = userInput.match(regex);
  for (const url of urls) {
    chrome.tabs.create({url: url});
  }
};


document.getElementById('collect-tabs-info').onclick = function() {
  chrome.tabs.query({}, function(tabs) {
    const tabsInfo = tabs.map(tab => `${tab.url},${tab.title}`);
    const tabsInfoText = tabsInfo.join('\n');

    navigator.clipboard.writeText(tabsInfoText).then(function() {
      alert('extract urls success');
    }, function(err) {
      console.error('extract urls fail', err);
    });
  });
};

popup.html

go 复制代码
<!DOCTYPE html>
<html>
  <head>
    <title>URL Opener</title>
    <style>
      body {
        width: 200px;
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <textarea id="user-input" rows="10" style="width: 100%;"></textarea>
    <button id="open-urls">Open URLs</button>
    <button id="collect-tabs-info">Extract URLs</button>
    <script src="popup.js"></script>
  </body>
</html>

manifest.json

go 复制代码
{
  "manifest_version": 3,
  "name": "URL Opener",
  "version": "1.0",
  "action": {
    "default_popup": "popup.html"
  },
  "permissions": [
	"tabs"
  ]
}

调试:

配合一些console的使用即可

相关推荐
军军君011 分钟前
Three.js基础功能学习七:加载器与管理器
开发语言·前端·javascript·学习·3d·threejs·三维
哈__3 分钟前
React Native 鸿蒙开发:内置 Share 模块实现无配置社交分享
javascript·react native·react.js
沐墨染26 分钟前
敏感词智能检索前端组件设计:树形组织过滤与多维数据分析
前端·javascript·vue.js·ui·数据挖掘·数据分析
为自己_带盐29 分钟前
在 Blazor Server 中集成 docx-preview.js 实现高保真 Word 预览
javascript·c#·word
Kapaseker1 小时前
前端已死...了吗
android·前端·javascript
Moment1 小时前
富文本编辑器技术选型,到底是 Prosemirror 还是 Tiptap 好 ❓❓❓
前端·javascript·面试
xkxnq1 小时前
第二阶段:Vue 组件化开发(第 18天)
前端·javascript·vue.js
晓得迷路了1 小时前
栗子前端技术周刊第 112 期 - Rspack 1.7、2025 JS 新星榜单、HTML 状态调查...
前端·javascript·html
怕浪猫1 小时前
React从入门到出门 第五章 React Router 配置与原理初探
前端·javascript·react.js
鹏多多1 小时前
前端2025年终总结:借着AI做大做强再创辉煌
前端·javascript