微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 Microsoft Edge 扩展中获取当前选项卡的 URL

如何解决如何从 Microsoft Edge 扩展中获取当前选项卡的 URL

我正在尝试边缘扩展,我正在尝试制作一个读取当前选项卡的 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。

ma​​nifest.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 文件等)。

enter image description here

MS Edge 89.0.774.45 的测试结果:

enter image description here

此外,您可以尝试根据自己的要求修改扩展程序的代码。

感谢您的理解。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。