是否可以将字符串从一个已打开的选项卡传输到另一个已打开的带有 Chrome 扩展名的选项卡?
Is it possible to transfer a string from one already open tab to another already opened tab with a Chrome extension?
好吧,我被困在这个问题中,因为我什至不知道从哪里开始搜索。
这是我要解决的问题:
我想自动化一些需要将值从一个站点“复制粘贴”到另一个站点(具有不同 URL)的过程,所以我想做一个扩展,我可以在其中找到一个 DOM 节点并获取其文本值并自动放入另一个页面的文本字段中。
我在 Mozilla 官方文档中搜索并发现“tabs.sendMessage”可能是我可以使用的东西,但我需要 tabs.id 以及我读过的内容“chrome.tabs”似乎只是来自扩展的 属性,所以我开始认为也许没有办法做到这一点。
我想学习如何执行此操作,因为它可以节省我的时间,使我无需将数据从一个选项卡传递到另一个选项卡,但需要更改一些内容。
感谢@wOxxOm,我找到了解决这个问题的方法,这是我得到的解决方案:
在我获取信息的页面上启动脚本的操作弹出窗口和在目标页面上运行的内容脚本,这里是 manifest.json:
{
"name": "autofill",
"description": "Writes info from one tab to another",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "first-page-background.js"
},
"permissions": ["storage", "activeTab", "scripting"],
"content_scripts": [
{
"matches": ["https://second-tab.com/"],
"js": ["second-tap-content.js"]
}
],
"action": {
"default_popup": "popup.html"
}
}
这是链接到“popup.html”的 javascript,我在其中更改了 chrome 存储变量:
document
.querySelector('xpath_to_element_i_want_trigger_action')
.addEventListener('click', () => {
let data = document.querySelector(
'xpath_to_data'
).innerText;
chrome.storage.sync.set({ form_data: data });
});
这是第二个选项卡脚本内容中的 运行 代码:
chrome.storage.onChanged.addListener(function (changes) {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
let searchBox = document.querySelector("#id_in_form)
searchBox.innerHTML = newValue
}
});
我希望这对其他人有帮助!
好吧,我被困在这个问题中,因为我什至不知道从哪里开始搜索。
这是我要解决的问题:
我想自动化一些需要将值从一个站点“复制粘贴”到另一个站点(具有不同 URL)的过程,所以我想做一个扩展,我可以在其中找到一个 DOM 节点并获取其文本值并自动放入另一个页面的文本字段中。
我在 Mozilla 官方文档中搜索并发现“tabs.sendMessage”可能是我可以使用的东西,但我需要 tabs.id 以及我读过的内容“chrome.tabs”似乎只是来自扩展的 属性,所以我开始认为也许没有办法做到这一点。
我想学习如何执行此操作,因为它可以节省我的时间,使我无需将数据从一个选项卡传递到另一个选项卡,但需要更改一些内容。
感谢@wOxxOm,我找到了解决这个问题的方法,这是我得到的解决方案:
在我获取信息的页面上启动脚本的操作弹出窗口和在目标页面上运行的内容脚本,这里是 manifest.json:
{
"name": "autofill",
"description": "Writes info from one tab to another",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "first-page-background.js"
},
"permissions": ["storage", "activeTab", "scripting"],
"content_scripts": [
{
"matches": ["https://second-tab.com/"],
"js": ["second-tap-content.js"]
}
],
"action": {
"default_popup": "popup.html"
}
}
这是链接到“popup.html”的 javascript,我在其中更改了 chrome 存储变量:
document
.querySelector('xpath_to_element_i_want_trigger_action')
.addEventListener('click', () => {
let data = document.querySelector(
'xpath_to_data'
).innerText;
chrome.storage.sync.set({ form_data: data });
});
这是第二个选项卡脚本内容中的 运行 代码:
chrome.storage.onChanged.addListener(function (changes) {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
let searchBox = document.querySelector("#id_in_form)
searchBox.innerHTML = newValue
}
});
我希望这对其他人有帮助!