【发布时间】:2023-03-07 11:39:01
【问题描述】:
我正在尝试为 Firefox 60.0.1 Quantum 编写我的第一个 WebExtension,并寻求您的帮助。
我试图简要描述一下我的 Firefox WebExtension。
步骤 1. 在浏览器面板中,按下扩展按钮,- 打开我的 扩展窗口。
第 2 步。在此扩展窗口中有一个 HTML 页面(文件
window.html) 带有一个名为“Active Tab URL” (id="url") 的文本字段。第 3 步。在主浏览器窗口中切换/更新选项卡时,此
文本字段应包含活动/打开选项卡的 URL。
我的问题:
- 如何(从 background.js)向扩展窗口的文本字段 (id="url") 传递任意值?
- 如何获取活动选项卡的 URL,以将其传输到我的扩展窗口?
我搜索了网站上的信息:https://developer.mozilla.org/en-US/Add-ons/WebExtensions 但不幸的是,我不明白如何做到这一点。
感谢您的帮助和想法!
manifest.json
{
"description": "Adds browser action icon to toolbar to open packaged web page.",
"manifest_version": 2,
"name": "Extension Window",
"version": "1.0",
"permissions": ["tabs", "contextMenus", "clipboardWrite", "clipboardRead"],
"icons": {"48": "icons/icon-48.png"},
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icons/icon-32.png"
}
}
background.js
function openMyWindow() {
var createWindow = {
type: "detached_panel",
url: "window.html",
titlePreface: "Extension Window",
width: 350,
height: 950
};
let windowId;
browser.windows.create(createWindow).then((data) => {
windowId = data.id;});
/*
1. How can I pass a any value to the text field (id="url") of my Extension Window?
2. How can I get the URL of the active Tab, to transfer it to my Extension Window?
*/
}
/*
Add openMyWindow() as a listener to clicks on the browser action.
*/
browser.browserAction.onClicked.addListener(openMyWindow);
window.html
<html>
<head>
<title>Extension Window</title>
<meta charset="utf-8">
</head>
<body>
<h1>My Extention Window</h1>
<p>Active Tab URL: <input id="url" type="text"></p>
</body>
</html>
【问题讨论】:
标签: javascript firefox firefox-addon firefox-addon-webextensions