Javascript 将window.location.href从扩展复制到剪贴板

Javascript 将window.location.href从扩展复制到剪贴板,javascript,html,safari-extension,Javascript,Html,Safari Extension,我正在尝试将“window.location.href”例如当前页面的URL从扩展名复制到剪贴板 我的问题是,当我将URL复制到剪贴板时,复制的是扩展URL,而不是我正在访问的页面 扩展栏: <!DOCTYPE HTML> <html> <head> <button onclick="copyFunction();">Copy</button> <script type="text/java

我正在尝试将“window.location.href”例如当前页面的URL从扩展名复制到剪贴板

我的问题是,当我将URL复制到剪贴板时,复制的是扩展URL,而不是我正在访问的页面

扩展栏:

<!DOCTYPE HTML>
<html>
    <head>
        <button onclick="copyFunction();">Copy</button>
        <script type="text/javascript">
          function copyFunction() {
          var inputDump = document.createElement('input'),
              hrefText = window.location.href;
          document.body.appendChild(inputDump);
          inputDump.value = hrefText;
          inputDump.select();
          document.execCommand('copy');
          document.body.removeChild(inputDump);
          }
        </script>
    </head>
</html>
<!DOCTYPE HTML>
<script>
    safari.application.addEventListener("command", copyFunction, false);

    function copyFunctionEvent(event) {
        if (event.command == "CopyToClipboard") {
            safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("CopyToClipboard", "all");

    }
}
</script>
function myextension_openAll(event){
    if (event.name == 'CopyToClipboard'){         
       function copyFunction() {
       var inputDump = document.createElement('input'),
           hrefText = window.location.href;
       document.body.appendChild(inputDump);
       inputDump.value = hrefText;
       inputDump.select();
       document.execCommand('copy');
       document.body.removeChild(inputDump);
       }

}
safari.self.addEventListener("message", myextension_openAll, true);
实际值: 狩猎-extension://com.myextension-0000000000/abc123/extensionbar.html

期望值: (例如,如果当前选项卡)

从上面的代码(Extensionbar html)中,您似乎编写了旧版Safari扩展(.safariextz),它已被弃用。看

我建议您通过以下过程将代码重写到Safari应用程序扩展中,该过程可以用Swift编写。我不知道为什么错误的URL会被复制到你的代码中的剪贴板上,但是重写你的代码会解决这个问题

创建应用程序扩展项目 按照[File]->[New]->[Project…]创建应用程序扩展,然后在Xcode上选择[Safari Extension App]。项目模板包含菜单栏实现的示例

通过单击菜单栏按钮复制location.href 以下代码将在您单击菜单栏按钮时向copy location.href添加功能

只需将其粘贴到SafariExtensionHandler.swift中

类SafariExtensionHandler:SFSafariExtensionHandler{
重写func messageReceived(withName messageName:String,from page:SFSafariPage,userInfo:[String:Any]?){
//当注入的脚本调用safari.extension.dispatchMessage时,消息将出现在这里
guard let href=userInfo?[“href”]as?字符串else{return}
//将href保存到剪贴板
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(href,forType:.string)
}
覆盖功能工具栏项已单击(在窗口中:SFSafariWindow){
//请求发送一条消息到location.href
window.getActiveTab{currentTab在
currentTab!.getActivePage{currentPage in
currentPage!.dispatchMessageToScript(名称为:“getHref”,用户信息:nil)
}
}
}
}
和注入脚本(script.js),如下所示

safari.self.addEventListener(“消息”,函数(事件){
控制台日志(“收到事件”);
safari.extension.dispatchMessage(“sendHref”,“href”:location.href});
});
工作示例 在这里完成工作代码,这可能有助于您的工作。祝你好运:)


也许吧?我对Safari扩展一无所知,这个答案是0 upvote,以防万一。感谢Jeto,我也尝试过这个方法,但主要问题是我不确定如何在extensionbar.html、global.html和注入脚本之间正确通信。恐怕我迷路了谢谢!这肯定会让我走上正确的方向。我现在似乎只需要读一些swift将此标记为答案。