Auto Refresh on load page failure - google-chrome

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!

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.).

Related

Chrome extension placeholder value for manifest field

I was wondering if it was possible to set a 'placeholder' value for a field in the manifest.json file of a Chrome extension. For example, I just started a new extension and my manifest looks like this :
{
"manifest_version": 2,
"author" : "My Name",
"name": "Extension Name",
"version": "0.0.1",
"description": "Desc",
"icons": { "16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png" },
"browser_action" : {
"default_title" : "Extension",
"default_icon" : {
"16": "icon32.png",
"48": "icon48.png",
"128": "icon128.png"
},
"default_popup" : "dropdown.html",
"js" : ["dropdown.js"]
},
"content_scripts" : [ {
"matches" : ["http://*/*, https://*/*"],
"js" : ["some_script.js"]
} ]
}
However, I have not yet made the icno*.png files so, obviously, whenever trying to load the extension, chrome says that it can't find the icon files.
Can I do something like :
//[...]
"default_icon" : {
//Will be added later
},
//[...]
or do I have to just delete the "browser_icon" and "icons" fields and add them later?
I found nothing in the Official Google Documentation.
In general, placeholder values are not permitted in the manifest.json. Many of the fields are checked for validity and/or that the resource is available when the manifest.json is loaded. This is necessary because these values are immediately used. Thus, they must be valid. The checking includes verifying the icon files for the browser_action and page_action keys. These files must exist for the extension to be loaded. Overall, what is required to be resolved, in order for the manifest.json to load, is explicitly stated in the error displayed when you attempt to load the unpacked extension from chrome://extensions/. You will need to resolve all errors in order for the extension to be loaded.
For icon files, you can usually delete the reference, or provide a placeholder icon file that contains an actual graphic. Personally, I just copy one of an assortment of icons I generally use, which are in the public domain, into a generically named icon file (e.g. icon.png, or myIcon.png, etc.). If you want, you could do a search for public domain icons which you could use for this purpose. There are a lot of choices.

Chrome Extension - Event Pages/Message Passing is not working

I'm novice in Google Chrome Extension development and I'm running through an issue which is causing the content script not getting executed. Below is a detailed description of the issue.
I'm working an extension to read some DOM content from a web site per say example.com I've the following files and the respective code part of it.
manifest
{
"manifest_version" : 2,
"name" : "My First Chrome App",
"description" : "My First Chrome App",
"version": "1.0",
"browser_action" : {
"default_title" : "Hello"
},
"permissions" : ["tabs"],
"background" : {
"scripts" : ["background.js"],
"persistence" : false
},
"content_scripts":[
{
"matches": [
"http://example.com/HomePage.aspx"
],
"js": ["jquery_224.js", "content_script.js"]
}]
}
background.js
My intention is to create a tab and surf to a page which is mentioned in the below script. And, it has to send a message to content_script.js
chrome.browserAction.onClicked.addListener(function(){
chrome.tabs.create({ url: "http://example.com/HomePage.aspx" }, function(tab){
chrome.runtime.sendMessage({authKey : "parse-dom"});
setTimeout(function(){
chrome.tabs.remove(tab.id);
}, 2000);
});
});.
content_script.js
Here I'm trying to read the authKey that I'm sending it from my background.js
chrome.runtime.onMessage.addListener(function(request,sender,response){
alert(request.authKey);
});
Unfortunately, I'm not getting the alert nor seeing any script errors. I have gone through the Chrome Messaging API and followed the same
Where am I going wrong?
Try with
"content_scripts":[
{
"run_at": "document_start",
More infos: https://developer.chrome.com/extensions/content_scripts
or try putting a timeout on your background sendMessage
EDIT: I've also noticed on manifest.json you have an HTTPS match while you are creating a tab with an HTTP address

Error on publishing Chrome Extension

I Have develop a Chrome extension but I'm stuck in the publishing step... When I try to upload my extension, I get this error :
An error occurred: Failed to process your item.
The manifest must define a version.
But I have defined a version... Here my manifest.json file :
{
"manifest_version": 2,
"name": "AccessID - RFID", // Nom
"version": "0.0.0.1", // Version
"version_name": "0.1 Beta",
"description": "Gestion RFID.", // Description
"icons": { ... },
"permissions": [ ... ],
"options_ui": { "page": ... },
"background":
{
"scripts": [ ... ]
},
"content_scripts" :
[
{
"matches": [...],
"all_frames": true,
"js": [ ... ],
"css" : [ ... ]
},
{
"matches": [ ... ],
"all_frames": true,
"js": [ ... ]
}
],
"web_accessible_resources" : [ ... ],
"page_action":
{
"default_title": "",
"default_popup": "",
"default_icon": { ... }
}
}
The most weird thing is that it's working great in developement mode. The extesnion is loaded and works well. So I actually don't understand what is going on.
manifest.json, as the name implies, is a JSON (JavaScript Object Notation) file and not a JavaScript source file.
The JSON format is deliberately simplistic and does not support any form of comments - so technically they are a syntax error. It's strange that it loaded normally for development, though.
Removing // comments will fix your issue. Other common issues include forgetting to put keys into quotation marks and leaving an extra comma - JavaScript would not complain about this since it's a valid ECMAScript object literal, but it's invalid JSON.
You can use JSON validators like JSONLint to catch errors like this.

Accessing the Cast API from within an extension page

I was working on a chromecast app, and I wanted to incorporate it into a chrome extension. I'm also using knockout.js in order to help with some of the UI. I have two pages, one is unsandboxed (http://jsfiddle.net/AuJaX/3/), and the other is sandboxed (http://jsfiddle.net/V2dJc/1/). None of the console.log's ever get called. My manifest is below:
{
"manifest_version": 2,
"name": "__MSG_app_title__",
"description": "__MSG_app_description__",
"version": "0.1",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/content/content.js"]
}
],
"background": {
"scripts": ["js/back/background.js"],
"persistent": false
},
"permissions": [
"tabs",
"contextMenus"
],
"page_action": {
"default_title": "__MSG_app_title__",
"default_icon": {
"19": "images/icon-19.png"
}
},
"sandbox": {
"pages": ["sandboxed.html"]
},
"icons": { "48": "images/icon.png" },
"default_locale": "en"
}
Is there anything that I'm doing wrong, or is this something that's not supported (yet??)?
Did you whitelist the domain you are trying to use the extension on? Currently to have the Cast API injected into the page you need two things:
<html data-cast-api-enabled="true">
and you need to follow the steps at the bottom of this page (whitelisting in the extension, not the same as the Google Cast device whitelisting):
https://developers.google.com/cast/whitelisting#whitelist-chrome
That said, I doubt this is going to work. The instructions are for getting the Cast API injected into a regular web page. However, if I'm not mistaken you want the API injected into your Chrome extension page. I don't know if it will be made available there, since I don't think two different extensions are allowed to interact.

Chromium extension: javascript not executed

Making a simple plugin , which executes javascript when a page is loaded , and I want to execute different javascript on different page
But the following code wasn't working as expected , no "alert" was triggered:
background.html:
<html><body>
<script>
chrome.webRequest.onCompleted.addListener(
function(details) {
alert (details.url);
},
{
urls: ["*"],
types: ["main_frame"]
},
[]
);
</script>
</body></html>
manifest.json:
{
"name": "JS",
"background_page": "background.html",
"permissions": [
"webRequest",
"*"
],
"version":"0.10"
}
Alerts and console.log made from the background page of an extension simply aren't visible on the general pages.
If you want to see them, you have to open the background page : Go to the extensions settings page (menu tools/extensions) and click the "background.html" link below the name of your extension.
In your case it may be better, during development phase, to simply add the console.log and alerts in the content scripts (i.e. not the background page). So you can read them without opening the background page.
EDIT : as requested, an extension that will simply alert the location :
main.js :
alert(document.location.href);
manifest.json :
{
"name": "Any name",
"version": "3.3",
"background_page": "background.html",
"content_scripts": [
{
"matches": ["http://*/*"],
"all_frames" : true,
"run_at" : "document_end",
"js": [
"main.js"
]
}
]
}
Yes I tested it. And yes it's as painful as it sounds. You should use console.log instead of alert during your dev.