manifest.json :清单文件 必须有,此插件配置清单,命名必须是manifest.json
images:放置插件图标
js:放置插件执行的js文件
{ "author": "Microsoft OCOS Team", "description": "Get information of the active tab.", "icons": { "48": "icons/microsoft.png", "96": "icons/microsoft-96.png" }, "manifest_version": 2, "name": "HelloWorld", "version": "1.0", "permissions": [ "tabs" ], "browser_action": { "default_icon": { "30": "icons/microsoft-30.png" }, "default_title": "HelloWorld", "default_popup": "GetTabInfo.html" } }
author, name, version三个必填
其中:matches字段必须要有,对匹配的网页注入. js对应js文件, run_at:何时注入 这里选择加载文当前
三、html文件
建个文件命名为“GetTabInfo.html”
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="GetTabInfo.css" /> </head> <body> <div class="tabInfo">Get Tab Info</div> <div id="info" style="display:none"></div> <script src="GetTabInfo.js" ></script> </body> </html>
再建一个脚本文件“GetTabInfo.js”, 贴入如下代码:
document.addEventListener("click", function(e) { if (!e.target.classList.contains("tabInfo")) { return; } var root = document.getElementById("info"); root.innerHTML = ""; browser.tabs.query({ active: true, currentWindow: true }, function (tabs) { browser.tabs.get(tabs[0].id, function (tab) { var node = document.createElement("div"); var textnode = document.createTextNode("Url: " + tab.url); node.appendChild(textnode); root.appendChild(node); var node2 = document.createElement("div"); var textnode2 = document.createTextNode("Title: " + tab.title); node2.appendChild(textnode2); root.appendChild(node2); }); root.style.display = "block"; }); });
这篇文档对您是否有帮助?