Chrome插件popup.js提取当前tabs页面地址url + 标题title

popup.js


chrome.tabs.getSelected(null, function (tab) { // 先获取当前页面的tabID
     
   alert(tab.title);
   alert(tab.url);
});
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的Chrome插件示例,它会在页面加载完成后自动向指定节点插入数据: 1. 创建一个新文件夹,命名为 "my-extension"。 2. 在 "my-extension" 文件夹中创建一个名为 "manifest.json" 的文件,并将以下内容复制到该文件中: ``` { "name": "My Extension", "version": "1.0", "manifest_version": 2, "description": "A simple extension that inserts data into a specific node on a page.", "content_scripts": [ { "matches": [ "<all_urls>" ], "js": [ "content.js" ] } ] } ``` 3. 在 "my-extension" 文件夹中创建一个名为 "content.js" 的文件,并将以下内容复制到该文件中: ``` // Wait for the page to finish loading window.addEventListener("load", function(event) { // Find the target node var targetNode = document.getElementById("target-node-id"); if (targetNode) { // Insert the data targetNode.insertAdjacentHTML("beforeend", "<p>Here is some data!</p>"); } }); ``` 4. 在 "my-extension" 文件夹中创建一个名为 "popup.html" 的文件,并将以下内容复制到该文件中: ``` <!DOCTYPE html> <html> <head> <title>My Extension</title> <script src="popup.js"></script> </head> <body> <h1>My Extension</h1> <p>Click the button to insert data into the target node.</p> <button id="insert-data">Insert Data</button> </body> </html> ``` 5. 在 "my-extension" 文件夹中创建一个名为 "popup.js" 的文件,并将以下内容复制到该文件中: ``` // Find the "Insert Data" button var insertDataButton = document.getElementById("insert-data"); // When the button is clicked, send a message to the content script to insert the data insertDataButton.addEventListener("click", function(event) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, {action: "insertData"}); }); }); ``` 6. 在 Chrome 浏览器中打开 "chrome://extensions/" 页面。 7. 选择 "开发者模式",然后点击 "加载已解压的扩展程序" 按钮。 8. 选择 "my-extension" 文件夹,然后点击 "确定"。 现在,当你在浏览网页时,可以单击插件图标并单击 "Insert Data" 按钮,以在页面中插入数据。请确保将 "target-node-id" 替换为实际要插入数据的节点的 ID。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值