admin管理员组文章数量:1420917
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
2 Answers
Reset to default 4As 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
本文标签:
版权声明:本文标题:javascript - Chrome Extension, is it possible to simulate "go back" from a background script? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745342374a2654321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论