Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Refresh on load page failure

I'm trying to create an extension for chrome that auto refreshes the page when the page load is failed for any reason.

my manifest.json:

{ "browser_action" : { "default_icon" : "icon.png"
    },
  "description" : "Making your first Google Chrome extension.",
  "icons" : { "128" : "icon.png" },
  "name" : "Tutorialzine Extension",
  "version" : "1.0", 
  "permissions": [
     "webRequest",
     "tabs",
     "<all_urls>"
   ],
  "content_scripts": [
    {
      "matches": ["<all_urls>","http://*/*","https://*/*","*://*/*"],
      "js": ["myscript.js"],
      "run_at": "document_end"
    }
  ]
}

myscript.js :

chrome.webRequest.onErrorOccurred.addListener(function details){
    chrome.tabs.reload(details.tabId);
}

What am I doing wrong? Thanks in advance!

like image 269
Avni Yayin Avatar asked Dec 04 '25 16:12

Avni Yayin


1 Answers

Content scripts don't have access to most of chrome.* APIs. It's clearly stated in the docs:

However, content scripts have some limitations. They cannot:
- Use chrome.* APIs (except for parts of chrome.extension)

You should use a background page or event page instead.

Also chrome.webRequest.onErrorOccurred.addListener(function details) is not a valid JavaScript code. function keyword shouldn't be there. I believe you copied this code from docs, but in docs this type of pseudo-JavaScript is used only to describe function definition (what types of arguments it expects, what type of values does it return etc.).

like image 127
Konrad Dzwinel Avatar answered Dec 07 '25 18:12

Konrad Dzwinel