问题描述
我正在尝试边缘扩展,我正在尝试制作一个读取当前选项卡的 URL,删除字符串 '%0A' 然后重定向到清理过的 URL,但我不知道如何读取当前标签 URL,我找到了如何在 chrome 中执行此操作:
chrome.tabs.query({active: true,currentwindow: true},function(tabs) {
// print object for debugging
console.log(JSON.stringify(tabs[0]));
// get active tab url
var activeTab = tabs[0];
var activeTabURL = activeTab.url;
alert(activeTabURL)
});
但它似乎不适用于 Edge,我有在清单中设置“选项卡”的权限。 希望你能帮帮我
解决方法
我建议您参考下面的示例,它可以帮助您从 Edge 浏览器扩展中获取当前选项卡的 URL。
manifest.json:
{
"name": "Hello World","version": "2.0.0","description": "Simple Microsoft Edge Extension","manifest_version": 2,"author": "abc","icons": {
"16": "icons/icon_16.png"
},"browser_action": {
"default_popup": "background.html","default_title": "Hello World"
},"permissions": [
"tabs","<all_urls>"
],"background": {
"page": "background.html","persistent": true
},"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],"css" : ["light.css"],"js": ["js/content.js"],"run_at": "document_end"
}
]
}
background.html:
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
</head>
<body>
<div>
<h3>Click the button to get the page URL...<h3><br>
<button id="btn1">click me</button>
<input type="text" id="txt1" style="width:300px">
</div>
<script type="text/javascript" src="js/background.js"></script>
</body>
</html>
background.js:
var btn= document.getElementById("btn1");
btn.addEventListener("click",function(){
abc();
});
function abc()
{
chrome.tabs.query({active: true,lastFocusedWindow: true},function(tabs)
{
var tab = tabs[0];
document.getElementById("txt1").value= tab.url;
});
}
扩展文件结构如下所示。您可以自己创建其他文件(如 CSS 文件等)。
MS Edge 89.0.774.45 的测试结果:
此外,您可以尝试根据自己的要求修改扩展程序的代码。
感谢您的理解。