简体   繁体   English

使用Chrome扩展程序获取当前网址

[英]Get current URL with Chrome Extension

I'm trying to write a Chrome extension that will take the URL of the page the user is on and send it to a server for a response back. 我正在尝试编写Chrome扩展程序,该扩展程序将获取用户所在页面的URL并将其发送到服务器以获取响应。

So far I have been trying to use chrome.tabs.getCurrent() , but I get uncaught TypeError on the getCurrent object. 到目前为止,我一直在尝试使用chrome.tabs.getCurrent() ,但是我在getCurrent对象上得到了未被捕获的TypeError。

Is there an easy way for doing this? 这样做有简单的方法吗?

Any reason why you don't want to use getSelected() ? 您不想使用getSelected()任何原因?

chrome.tabs.getSelected(windowId, function(tab) {
    alert("current:"+tab.url);
});

getSelected is deprecated. 不推荐使用getSelected。 The preferred way to access the current tab is: 访问当前选项卡的首选方法是:

    chrome.tabs.query({active: true}, function(tab) {
        // Do stuff here
    }

You are getting the error because getCurrent returns the tab the script is running on, not the tab that is currently selected. 您收到错误,因为getCurrent返回运行脚本的选项卡,而不是当前选定的选项卡。

You should probably be using getSelected as noted by serg 你可能应该使用serg所指出的getSelected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM