javascript - Chrome Extension, is it possible to simulate "go back" from a background script? - Stack Overflow

Here is what I have now in a background script:chrome.tabs.onUpdated.addListener(function(tabId, change

Here is what I have now in a background script:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
        if (tab.url.indexOf("developer.apple/reference") != -1 && tab.url.indexOf("?language=objc") == -1) {
            var objcURL = tab.url + "?language=objc";
            history.back();
        }
});

I am entering the if block properly, but history.back() doesn't appear to do anything. I've tried history.go(-1), and I am positive that I have "history" set in the permissions of my manifest

Is this not possible to do from a background script in a chrome extension?

Here is what I have now in a background script:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
        if (tab.url.indexOf("developer.apple./reference") != -1 && tab.url.indexOf("?language=objc") == -1) {
            var objcURL = tab.url + "?language=objc";
            history.back();
        }
});

I am entering the if block properly, but history.back() doesn't appear to do anything. I've tried history.go(-1), and I am positive that I have "history" set in the permissions of my manifest

Is this not possible to do from a background script in a chrome extension?

Share Improve this question asked Nov 30, 2016 at 16:17 A OA O 5,6983 gold badges38 silver badges73 bronze badges 2
  • I'm not sure if it is possible in background script but try using content script that will inject a script that will load the previous page. See the related SO post, this explain how to inject code in a page using a Content script. Hope this helps. – Mr.Rebot Commented Dec 1, 2016 at 15:43
  • what if your listener event is not firing at all? – Bekim Bacaj Commented Dec 10, 2016 at 4:40
Add a ment  | 

2 Answers 2

Reset to default 4

As Mr. Rebot was saying, the problem is that you are executing the history.back(); in the background script not the tab you want to go back in. A simple work around is to use chrome.tabs.executeScript to run the js in the current tab. So all you need to do is replace history.back(); with chrome.tabs.executeScript(null,{"code": "window.history.back()"}); Also, the history permission allows you to read and write the browser history so you do not need it for this to work. You do however need the following permissions to inject the code: [ "tabs", "http://*/*", "https://*/*" ] Finally, I have to say use chrome.tabs.executeScript carefully, its not much better than eval() but as long as you don't replace the window.history.back(); with a variable you should be ok. Also, if you want to run the code in a page other than the current page you can replace null with the id of the tab you want to inject the code into. Further documentation can be found on the chrome API page found here.

Since Chrome version 72 (released in January 2019), there are two new methods on the tabs API, goBack() and goForward().

chrome.tabs.goBack(tabId);

https://developer.chrome./docs/extensions/reference/api/tabs#method-goBack

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342374a4623386.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信