Javascript 如何通过扩展修改 Chrome 中的当前 url 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1891738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:47:14  来源:igfitidea点击:

How to modify current url location in chrome via extensions

javascriptgoogle-chromegoogle-chrome-extension

提问by Dan Atkinson

I want to create an extension that redirects the user to another website if he clicks on the extension button. So far I have only seen extensions which create a new tab for each click.

我想创建一个扩展程序,如果用户单击扩展程序按钮,则将用户重定向到另一个网站。到目前为止,我只看到了为每次点击创建一个新标签的扩展。

Is it possible to redirect the user to another website using the active tab?

是否可以使用活动选项卡将用户重定向到另一个网站?

I tried something like this:

我试过这样的事情:

chrome.browserAction.onClicked.addListener(function(tab) {
    var url = "https://www.mipanga.com/Content/Submit?url="
        + encodeURIComponent(tab.url)
        + "&title=" + encodeURIComponent(tab.title);

    document.location.href = url; // <-- this does not work
});

回答by Dan Atkinson

Attention:If you develop cross-browser extensions (I hope you do!), I recommend that you use chrome.tabs.query(). Please see Jean-Marc Amon's answerfor more information. This answer still works in both Firefox and Chrome, but query()is more commonly used, has more options, and works in background pages and popup views.

注意:如果您开发跨浏览器扩展(我希望您这样做!),我建议您使用chrome.tabs.query(). 有关更多信息,请参阅Jean-Marc Amon 的回答。这个答案仍然适用于 Firefox 和 Chrome,但query()更常用,有更多选项,并且适用于后台页面和弹出视图。

From the chrome.tabsAPI, you can use getCurrent()or query().

chrome.tabsAPI 中,您可以使用getCurrent()query()

I prefer getCurrentbut it cannot be called from a non-tab context (eg a background page or popup view). If this is a problem for you, you should look to use queryinstead. Jean-Marc Amon's answerbelow provides a wonderful example of how to get the active tab in this case (don't forget to upvote him!).

我更喜欢getCurrent但它不能从非选项卡上下文(例如背景页面或弹出视图)中调用。如果这对您来说是个问题,您应该考虑使用它query。下面让-马克·阿蒙 (Jean-Marc Amon) 的回答提供了一个很好的示例,说明在这种情况下如何获取活动选项卡(不要忘记给他点赞!)。

Once you have the current tab, simply pass update().

拥有当前选项卡后,只需通过update().

chrome.tabs.getCurrent(function (tab) {
  //Your code below...
  var tabUrl = encodeURIComponent(tab.url);
  var tabTitle = encodeURIComponent(tab.title);
  var myNewUrl = "https://www.mipanga.com/Content/Submit?url=" + tabUrl + "&title=" + tabTitle;

  //Update the url here.
  chrome.tabs.update(tab.id, {url: myNewUrl});
});

NB:In order to use this this functionality, you mustensure that you have the tabspermission enabled in your manifest.jsonfile:

注意:为了使用此功能,您必须确保tabs在您的manifest.json文件中启用了权限:

"permissions": [
  "tabs"
],

回答by Jean-Marc Amon

You can use chrome.tabs.query too

你也可以使用 chrome.tabs.query

chrome.tabs.query({currentWindow: true, active: true}, function (tab) {
      chrome.tabs.update(tab.id, {url: your_new_url});
});

回答by Domino

The chrome.tabs.updatemethod will automatically run on the current active tab if no tab id is passed.

chrome.tabs.update如果没有传递选项卡 ID,该方法将自动在当前活动选项卡上运行。

This has the added advantage of not requiring the tabspermission. Extensions with this permission warn the user that they can read the browsing history, so you should avoid asking for it if you don't need to.

这具有不需要tabs许可的额外优势。具有此权限的扩展程序会警告用户他们可以阅读浏览历史记录,因此如果您不需要,您应该避免要求它。

Changing the current tab's URL is as simple as writing this:

更改当前选项卡的 URL 就像编写以下内容一样简单:

chrome.tabs.update(undefined, {url: 'http://example.com'});

Or as mentionned by farwayer in the comments, you don't need to put two arguments at all.

或者正如 farwayer 在评论中提到的,你根本不需要提出两个论点。

chrome.tabs.update({url: 'http://example.com'});