How to make a sidebar in chrome extension?

I learned to create a chrome extension. I tried hi world example and it worked fine. Now I'm trying to add custom code and make some changes to the hello world code according to my requirements.

What I'm trying to create is when the user clicks on the icon in the address bar, he should open popup.html below the address bar , as shown in the figure. A screenshot is an extension called raindrop.io

They do this inside the chrome extension. When I click on the icon, it opens the right box on top of the existing web page and under the address bar to show all my saved bookmarks. I wanted to achieve the same effect, but I do not know where to start. I heard that there is an experimental sidebar, but Google has removed it.

Extension Raindrop.io

EDIT

I accepted the proposals and tried to implement this. Now I'm stuck in two places -

  • How to open a window by clicking on the icon in the address bar. Right now, it just opens automatically by itself. I want it to be open when the user clicks on the icon.
  • I do all this to create a note with an extension, and I did create notes, but it works in a pop-up interface, but I wanted to implement it in the sidebar interface.

Here is my code for -

a. Side extension interface in Chrome extension

manifest.json

{ "manifest_version": 2, "name": "Hello World", "description": "This extension to test html injection", "version": "1.0", "content_scripts": [{ "run_at": "document_end", "matches": [ "https://*/*", "http://*/*" ], "js": ["js/jquery-1.11.3.js", "js/content-script.js"], "css": ["css/custom.css"] }], "browser_action": { "default_icon": "icon.png" }, "permissions": [ "activeTab", "https://ajax.googleapis.com/" ] } 

Script.js Content

 var iframe = document.createElement('iframe'); iframe.style.background = "green"; iframe.style.height = "100%"; iframe.style.width = "360px"; iframe.style.position = "fixed"; iframe.style.top = "0px"; iframe.style.right = "0px"; iframe.style.zIndex = "9000000000000000000"; iframe.frameBorder = "none"; document.body.appendChild(iframe); 

B. Note. Accept Application Extension

popup.html

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>SideNotes</title> <link rel="stylesheet" href="css/style.css"> <script src="popup.js"></script> </head> <body> <div class="container"> <div id="toolbar"> <p id="title">SIDENOTES </p> <img id="logo" src="image/icon.png" alt=""> </div> <div id="all-notes"> <ul id="todo-items"></ul> </div> <div id="take-note"> <form id="new-todo-form" method="POST" action="#"> <textarea id="new-todo"></textarea> <input type="image" src="image/done.svg" id="submitButton"> </form> </div> </div> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/custom.js"></script> <script type="text/javascript" src="js/db.js"></script> <script type="text/javascript" src="js/app.js"></script> </body> </html> 

Screenshot of my application, it works locally enter image description here

+5
source share
1 answer

The chrome extension API does not have a right pane.

You can do this the same way the extension of your example does this:

  • Create background.js listening messages from the tab
  • Create content script sends a message to background.js if the tab is injection (if you need your extension work on system pages)
  • If the tab is injected, chrome.tabs.executeScript enter your div into the menu / listener input that will open your menu.
  • Profit

Read more on how to do this below.

  • Create background.js listening to browser browser icons and report your click script content.
  • Prevent popup.html showing by default popup.

manifest.js

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

background.js

 chrome.browserAction.onClicked.addListener(function(){ chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ chrome.tabs.sendMessage(tabs[0].id,"toggle"); }) }); 
  1. In the script.js content, create an invisible iframe, with your popup.html (with zero width type display:none; ). I use zero width due to the fact that you may need to animate the display of your menu using jquery, for example, an extension for example.
  2. In the contents of the script, add a message listener to receive messages sent from background.js script.
  3. When you receive a message, show or hide the menu block

content script.js

 chrome.runtime.onMessage.addListener(function(msg, sender){ if(msg == "toggle"){ toggle(); } }) var iframe = document.createElement('iframe'); iframe.style.background = "green"; iframe.style.height = "100%"; iframe.style.width = "0px"; iframe.style.position = "fixed"; iframe.style.top = "0px"; iframe.style.right = "0px"; iframe.style.zIndex = "9000000000000000000"; iframe.frameBorder = "none"; iframe.src = chrome.extension.getURL("popup.html") document.body.appendChild(iframe); function toggle(){ if(iframe.style.width == "0px"){ iframe.style.width="400px"; } else{ iframe.style.width="0px"; } } 
  1. Create popup.html and scripts that you load from the extension context visible to the html browser context:

manifest.json

 "web_accessible_resources": ["popup.html"] 

More details

+6
source

All Articles