Slide 1

Slide 1 text

CHROME EXTENSIONS

Slide 2

Slide 2 text

WHAT ARE CHROME EXTENSIONS?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

HTML/CSS/JAVASCRIPT We know this!

Slide 5

Slide 5 text

CHROME EXTENSION STRUCTURE

Slide 6

Slide 6 text

THE MANIFEST FILE {! ! "name": “My cool extension name",! ! "version": "0.0.0",! ! "author": "Nick Nisi",! ! "description": “NebraskaJS demo application",! ! ! "manifest_version": 2,! ! ! "permissions": [! ! ! "",! ! ! "storage"! ! ],! ! ! "content_scripts": [! ! ],! ! ! "background": {! ! },! ! ! "web_accessible_resources": [! ! ]! }! manifest.json

Slide 7

Slide 7 text

EXTENSION STRUCTURE manifest.json Page Content Scripts background page(s) Devtools Pages Browser Actions Page Actions

Slide 8

Slide 8 text

CONTENT SCRIPTS

Slide 9

Slide 9 text

CONTENT SCRIPTS • HTML/CSS TO EXECUTE ON THE PAGE • ACCESS TO DOM • LIMITED ACCESS TO chrome.* APIS "content_scripts": [! ! {! ! ! "matches": [ "http://*/*" ],! ! ! "css": [ "css/app.css" ],! ! ! "js": [ "scripts/contentscript.js" ],! ! ! "run_at": "document_end"! ! }! ],! manifest.json

Slide 10

Slide 10 text

CONTENT SCRIPTS DO NOT HAVE ACCESS TO THE JAVASCRIPT ON THE PAGE

Slide 11

Slide 11 text

Page DOM Content Scripts Page JavaScript CONTENT SCRIPT ACCESS

Slide 12

Slide 12 text

ACCESSING PAGE JAVASCRIPT function injectScript(url) {! ! var script = document.createElement('script');! ! script.type = 'text/javascript';! ! script.src = url;! ! ! document.body.appendChild(script);! }! ! injectScript(chrome.extension.getURL('scripts/inject.js'));! contentscript.js "web_accessible_resources": [! ! "scripts/inject.js"! ]! manifest.json

Slide 13

Slide 13 text

COMMUNICATING WITH CONTENT SCRIPTS window.addEventListener('message', function (event) {! ! if (event.source !== window) { return; }! ! ! if (event.data.type && event.data.from === 'page') {! ! ! console.log('Content script received: ', JSON.parse(event.data));! ! }! });! contentscript.js // send a message to the content script! window.postMessage({! ! from: 'page',! ! data: JSON.stringify(myDataToPass)! }, '*');! inject.js

Slide 14

Slide 14 text

BACKGROUND/EVENT PAGES

Slide 15

Slide 15 text

• ACCESS TO chrome.* APIS • LONG-RUNNING SCRIPTS FOR MANAGING EXTENSION STATE • BACKGROUND PAGES BECOME EVENT PAGES WHEN persistent: false • EVENT PAGES ARE LOADED ONLY WHEN NEEDED • MEMORY CAN BE FREED WHEN NOT IN USE BACKGROUND/EVENT PAGES "background": {! ! "scripts": ["scripts/background.js"]! ! "persistent": false! },! manifest.json

Slide 16

Slide 16 text

COMMUNICATION WITH BACKGROUND/EVENT PAGES chrome.extension.onConnect.addListener(function (port) {! ! if (port.name !== 'goodman-script') { return; }! ! ! port.onMessage.addListener(function (message) {! ! ! if (message.type === 'getQuote') {! ! ! ! getQuote().then(function (quote) {! ! ! ! ! port.postMessage({! ! ! ! ! ! type: 'quote',! ! ! ! ! ! quote: quote! ! ! ! ! });! ! ! ! });! ! ! }! ! });! });! background.js // open long-running connection with background script! var port = chrome.runtime.connect({ name: 'goodman-script' });! ! // listen for messages from background script! port.onMessage.addListener(function (data) {! ! if (data.type === 'quote') {! ! ! console.log(data.quote);! ! }! });! ! // post a message to the background page! port.postMessage({ type: 'getQuote' });! contentscript.js

Slide 17

Slide 17 text

BROWSER/PAGE ACTIONS

Slide 18

Slide 18 text

BROWSER ACTIONS "browser_action": {! ! "default_icon": {! ! ! "19": "img/goodman16.png",! ! ! "38": "img/goodman32.png"! ! },! ! "default_title": "John Goodman",! ! "default_popup": "popup.html"! },! manifest.json

Slide 19

Slide 19 text

PAGE ACTIONS "page_action": {! ! "default_icon": {! ! ! "19": "img/goodman16.png",! ! ! "38": "img/goodman32.png"! ! },! ! "default_title": "John Goodman",! ! "default_popup": "popup.html"! },! manifest.json

Slide 20

Slide 20 text

WHAT CAN YOU DO WITH CHROME EXTENSIONS?

Slide 21

Slide 21 text

APPLY YOUR OWN STYLES "content_scripts": [! {! "matches": [ "http://nebraskajs.com/*" ],! "css": [ "css/moregoodman.css" ]! }! ],! manifest.json

Slide 22

Slide 22 text

CREATE APPLICATIONS WITH WEB TECHNOLOGIES chrome.app.runtime.onLaunched.addListener(function() {! chrome.app.window.create('main.html', {! id: 'MyWindowID',! bounds: {! width: 800,! height: 600,! left: 100,! top: 100! },! minWidth: 800,! minHeight: 600! });! }); background.js

Slide 23

Slide 23 text

DEVTOOLS EXTENSIONS ! "devtools_page": "devtools.html" manifest.json ! ! ! ! ! devtools.html // create a new tab in the devtools! chrome.devtools.panels.create('Goodman Inspector', 'img/goodman.png', 'panel.html');! ! // create a sidebar in the elements panel! chrome.devtools.panels.elements.createSidebarPane('John Goodman Properties', function (sidebar) {! ! sidebar.setObject({! ! ! wasInRoseanne: true,! ! ! isAwesome: true,! ! ! bestMovie: 'Coyote Ugly'! ! }, 'John Goodman Properties');! });! devtools.js

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

NEBRASKAJS CHROME EXTENSIONS • JMHOBBS/DOMAIN-SWAP • CARLZULAUF/EXTENSIONS_DEVELOPER_CONSOLE

Slide 26

Slide 26 text

THERE IS A LOT YOU CAN DO

Slide 27

Slide 27 text

THERE IS A LOT YOU CAN CONFIGURE And a lot can go wrong

Slide 28

Slide 28 text

BUT IT’S EASY! And fun!

Slide 29

Slide 29 text

THANKS! THANKS!