Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extensions creating a .html page accessible by clicking browser action.

I've been trying to look around the web and have had difficulties finding an answer. I've seen a couple of web extensions such as OneTab that open a local index.html page when you click the browser action button. Does anyone know how I can get my chrome extension to open an index.html page when browser action is pressed?

like image 893
Frederik Petersen Avatar asked Mar 07 '16 01:03

Frederik Petersen


2 Answers

There are two ways to open your local HTML page by a browser action.

1. As a popup

manifest.json

"browser_action":   {
                            "default_icon"  :   "128.png",
                            "default_popup" :   "localPage.html",
                            "default_title" :   "localPage title"
                        }

2. As a normal page in Google Chrome browser

manifest.json

"background": {
        "scripts": ["background.js"]
    },

background.js

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.create({ url: chrome.runtime.getURL("localpage.html") });
});
like image 81
Satish Kumar sonker Avatar answered Oct 19 '22 18:10

Satish Kumar sonker


You should take a look at chrome.browserAction and chrome.runtime.getURL.

manifest.json

{
    "name": "Your Extension Name",
    "description": "Your Extension Description",
    "version": "0.1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "browser_action": {
         "title": "Your Browser Action Name"
    }
}

background.js

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.create({ url: chrome.runtime.getURL("index.html") });
});
like image 20
Haibara Ai Avatar answered Oct 19 '22 19:10

Haibara Ai