Javascript 复制DOM中的数据显示在Popup.html中(谷歌扩展)
我正在寻找Popup.html按钮内的onclick,复制网站元素数据,并在Popup.html ID内设置数据,该ID当前为N/a manifest.jsonJavascript 复制DOM中的数据显示在Popup.html中(谷歌扩展),javascript,html,jquery,google-chrome-extension,Javascript,Html,Jquery,Google Chrome Extension,我正在寻找Popup.html按钮内的onclick,复制网站元素数据,并在Popup.html ID内设置数据,该ID当前为N/a manifest.json { "name": "Copy Data in DOM", "description": "Extension ", "version": "1.0", "manifest_
{
"name": "Copy Data in DOM",
"description": "Extension ",
"version": "1.0",
"manifest_version": 3,
"permissions": ["storage", "activeTab", "scripting"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
}
},
"icons": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
}
}
content.js
document.getElementById("copySecondIddata").value;
document.getElementById("copyFirstIddata").value;
popup.html
<html>
<body>
<p>Fist Id Data: <span id="domFirstIdData">N/a</span></p>
<p>Fist Id Data: <span id="domSecondIdData">N/a</span></p>
<button type="button" id="setdata">Seat Data</button>
<script src="popup.js"></script>
</body>
</html>
执行文件(或代码)中的最后一个表达式将自动传递给executeScript的回调 content.js:已删除 manifest.json:
“权限”:[“脚本”、“活动选项卡”]
popup.js:
document.getElementById('setdata')。onclick=async()=>{
试一试{
const[tab]=await chrome.tabs.query({active:true,currentWindow:true});
const res=await chrome.scripting.executeScript({
目标:{tabId:tab.id},
功能:()=>[
document.getElementById('copyFirstIddata')。值,
document.getElementById('copySecondIddata')。值,
],
});
res=res[0]。result;//第一个元素是选项卡的主页面
document.getElementById('domFirstIddata')。value=res[0];
document.getElementById('domSecondIddata')。value=res[1];
}捕获(e){}
};
我不知道要发生什么,但是,这一行chrome.tabs.executeScript(null,{file:'content.js'},res=>
代码显示错误”,这是扩展区域中的“未捕获类型错误:chrome.tabs.executeScript不是函数”。@wOxxOmIn ManifestV3它已重命名,所以使用更新的答案。
function idTarget() {
chrome.tabs.executeScript(null, { file: "content.js" });
};
document.getElementById("setdata").onclick = idTarget;