Giter VIP home page Giter VIP logo

sample-extension's Introduction

sample-extension

This sample Chrome Extension shows how to use the different scripts available to Chrome extensions to access the JavaScript environment and DOM of a user's page.

What it can do?

  • know when it is being used and set the icon in the Chrome toolbar
  • add a global variable to the JavaScript environment of the user's page
  • read the DOM of the user's page
  • display the Element tree in a Panel in devtools

What scripts are used?

background script

A background script is used to listen for messages from other scripts. As the name indicates, this runs in the background and is always available โ€” even if no devtools windows are open.

This is executed by setting the following properties in the extension's manifest:

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

All this simple background script does is change the icon displayed in the Chrome toolbar when the extension is in use.

content script

A content script is also set up through the manifest using this configuration:

"content_scripts": [{
    "all_frames": true,
    "js": [ "content.js" ],
    "matches": [ "*://*/*" ]
}]

This script is injected by the extension into each frame of the page. It can access the DOM of the user's page, but runs in an isolated JavaScript environment.

The content script can also use chrome.* APIs as explained in the extensions docs.

Note: although not shown in this example, the content script could pass messages to the background script or other parts of the extension through the chrome.runtime.connect API.

injected script

The content script also injects a <script> tag into the users page. This injected script has access to the DOM and also the JavaScript environment of the user's page.

This script sets up a global variable (window.__SAMPLE_EXTENSION_NAMESPACE__) that contains functions that can be used by devtools page (explained below).

Note: although not shown in this example, the injected script could pass messages to the content script through window.postMessage.

devtools_page

Not technically a script, but this html page loads a JavaScript file that extends Chrome's devtools with a new panel.

This panel uses the chrome.devtools.inspectedWindow.eval to call functions on the __SAMPLE_EXTENSION_NAMESPACE__ object to retrieve data from the user's page and display it in the devtools panel.

How do I run it?

  1. clone the repo
  2. navigate Chrome to chrome://extensions/
  3. click "Load Unpacked"
  4. select the directory where the repo is cloned
  5. open Chrome devtools and click on the "Sample Extension" panel

sample-extension's People

Contributors

pravchuk avatar phillipskevin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.