Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload certain tab in chrome extension

I am trying to reload a page that a user may have open in one of their tabs from popup (specifically popup.js). I am looking at this answer but it is only for the current tab https://stackoverflow.com/a/25246060/8786209.

Also when it runs I get this error:

popup.html:1 Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "thewebpageiamtryingtoaccess.com".
Extension manifest must request permission to access this host at Object.callback (chrome-extension://mjckbfpnokoplldjpijdfhffbbbfflah/popup.js:3:17)

I have added

"permissions": [
    "tabs"
  ],

inside my manifest, still no luck. How would I be able to do it for a specific tab that the user has open. I want this to reload the website the first time the extension is loaded so the the user does not have to reload the webpage manually to have the content script take effect. Thanks!

like image 668
enzobarrett Avatar asked Jan 12 '18 05:01

enzobarrett


People also ask

How do I refresh a page extension?

It is very easy and super simple - just set the time interval that you want to auto refresh your pages or tabs and the Page-refresh extension will auto reload the pages.

Can you reattach a tab in Chrome?

Bring back a tab or window If you accidentally close a tab or window, you can open it again using a keyboard shortcut: Windows & Linux: Ctrl + Shift + t. Mac: ⌘ + Shift + t.

Is there an auto refresh extension?

Tab Auto Refresh is a browser addon that helps you automatically reload (refresh) tabs of your choice. To operate with this addon, please open toolbar popup UI while you are visiting a website. Adjust time interval in seconds (i.e. 120 sec) and you are all set.


1 Answers

For fixing execute error you need to change permissions in your manifest file to:

  "permissions": [
     "tabs",
     "http://*/",
     "https://*/"
  ]

You can query ID of the tab by url like this:

chrome.tabs.query({url: "http://thewebpageiamtryingtoaccess.com/*"}, function(tab) {
   // reload tab with one of the methods from linked answer
   chrome.tabs.reload(tab[0].id) 
})
like image 112
Sergii Rudenko Avatar answered Oct 25 '22 13:10

Sergii Rudenko