< Return to Video

11.5: Chrome Extensions: Pop-ups - Programming with Text

  • 0:00 - 0:04
    Hello. This is another video in my series
  • 0:04 - 0:07
    about Chrome extensions, which, you know,
  • 0:07 - 0:08
    right now, I just want to say at the
  • 0:08 - 0:10
    beginning of this video, I hope to
  • 0:10 - 0:12
    actually turn this playlist into
  • 0:12 - 0:13
    something that's, instead of saying
  • 0:13 - 0:15
    Chrome extensions, just say browser
  • 0:15 - 0:17
    extensions, because that is something
  • 0:17 - 0:19
    that is quite possible beyond simply the
  • 0:19 - 0:21
    browser Chrome. But I'm gonna stick with
  • 0:21 - 0:22
    Chrome right now as I work through all
  • 0:22 - 0:26
    the details. So what am I going to add in
  • 0:26 - 0:28
    this particular video? Well, as you can
  • 0:28 - 0:32
    see here in my diagram – I can't see over
  • 0:32 - 0:34
    there anymore. My monitor is now blocked
  • 0:34 - 0:36
    by something else. As you can see in my
  • 0:36 - 0:38
    diagram here, I have talked about content
  • 0:38 - 0:40
    scripts and background scripts. And what
  • 0:40 - 0:41
    I'm going to add today is something
  • 0:41 - 0:47
    called a pop-up. So a pop-up is a page
  • 0:47 - 0:49
    that you can launch that's essentially
  • 0:49 - 0:51
    like an overlay of what's in the
  • 0:51 - 0:53
    browser at a certain
  • 0:53 - 0:56
    moment. And I can launch it via a browser
  • 0:56 - 0:58
    action – for example, clicking a button
  • 0:58 - 1:01
    that I have in the top over here. I can
  • 1:01 - 1:03
    press that button and I can have some
  • 1:03 - 1:05
    sort of pop-up come up. So I could do
  • 1:05 - 1:07
    things like, Oh, I could have an interface
  • 1:07 - 1:08
    where I could change colors or make
  • 1:08 - 1:11
    selections or type in a word that I want
  • 1:11 - 1:13
    to look up. I could augment the browsing
  • 1:13 - 1:16
    experience through a separate pop-up
  • 1:16 - 1:18
    page that is that is part of my Chrome
  • 1:18 - 1:20
    extension. So that's what we're gonna
  • 1:20 - 1:20
    make
  • 1:20 - 1:23
    in this tutorial. Okay. So what I'm gonna
  • 1:23 - 1:24
    do is I'm gonna start with the code that
  • 1:24 - 1:26
    I had at the end of my background script
  • 1:26 - 1:30
    tutorial, which, if we look at – and I already
  • 1:30 - 1:31
    made a copy of it into a new folder,
  • 1:31 - 1:34
    which I'm calling pop-ups, which you can
  • 1:34 - 1:35
    find a link to this code for this
  • 1:35 - 1:39
    tutorial in the description as usual. And
  • 1:39 - 1:41
    what I have is that
  • 1:41 - 1:43
    manifest.json file. So i've already
  • 1:43 - 1:44
    changed it to Coding Train Extension 3.
  • 1:44 - 1:47
    Maybe I'll say, You know, I'll change it to The
  • 1:47 - 1:50
    Coding Train Pop-up. It is –
  • 1:50 - 1:53
    The content script is available for all URLs,
  • 1:53 - 1:55
    and the background script – Now
  • 1:55 - 1:57
    interestingly enough, I actually don't
  • 1:57 - 1:59
    need a background script just to have a
  • 1:59 - 2:01
    pop-up. Like, I might need a background
  • 2:01 - 2:02
    script for something else that's going
  • 2:02 - 2:03
    on in my extension, but I don't need it
  • 2:03 - 2:05
    just for the pop-up. So I think just for
  • 2:05 - 2:06
    simplicity in this particular tutorial,
  • 2:06 - 2:09
    I'm gonna take out the background script
  • 2:09 - 2:11
    and I'm actually going to just delete
  • 2:11 - 2:13
    the file that is the background script.
  • 2:13 - 2:14
    Actually, you know what? I'm gonna leave
  • 2:14 - 2:16
    it there, because eventually, one thing
  • 2:16 - 2:19
    that I'm gonna want to do is if you
  • 2:19 - 2:22
    recall, a way that I sent a message – the
  • 2:22 - 2:24
    way that I controlled the content script
  • 2:24 - 2:27
    or made an event happen in the content
  • 2:27 - 2:28
    script from the background script was
  • 2:28 - 2:30
    sending a message. And actually, what I'm
  • 2:30 - 2:32
    probably going to want to do now is send
  • 2:32 - 2:33
    a message from the pop-up to the content
  • 2:33 - 2:35
    script. So, the code that's in the
  • 2:35 - 2:36
    background script is going to become
  • 2:36 - 2:38
    useful for me, because I can use that
  • 2:38 - 2:43
    same code in the pop-up. All right. So – but –
  • 2:43 - 2:45
    when there's a browser action, I want
  • 2:45 - 2:48
    there to be something more. I want there
  • 2:48 - 2:54
    to be a default pop-up. No, it's not
  • 2:54 - 2:55
    written over there what I'm supposed to
  • 2:55 - 2:56
    type right here. I just looked over there
  • 2:56 - 2:57
    because there was something interesting
  • 2:57 - 3:00
    happening. I'm putting notes over there,
  • 3:00 - 3:04
    and I want this to be – Now can I make
  • 3:04 - 3:06
    that in another folder? Let's try this.
  • 3:06 - 3:08
    What I want to try to do is make the
  • 3:08 - 3:10
    pop-up an entire piece – Let's actually
  • 3:10 - 3:12
    first just make it an HTML page. So I'm
  • 3:12 - 3:16
    gonna just call it pop-up.html. And I
  • 3:16 - 3:19
    think I need to say – Oh, I don't need to
  • 3:19 - 3:24
    but I can say default title, and this
  • 3:24 - 3:26
    will appear in, like, the tooltips, like a
  • 3:26 - 3:30
    pop-up will come here. All right. So, I think
  • 3:30 - 3:35
    that is now some stuff that I can add to
  • 3:35 - 3:37
    now have a pop-up. Now, what this means is
  • 3:37 - 3:39
    there needs to be an HTML page called
  • 3:39 - 3:41
    pop-up.html. So I'm gonna make that
  • 3:41 - 3:45
    file – pop-up.html, and I'm just gonna
  • 3:45 - 3:47
    I mean, I should – I don't really know if you
  • 3:47 - 3:49
    need all this stuff in the HTML page, but
  • 3:49 - 3:52
    let's just put basically like something
  • 3:52 - 3:54
    really basic there right now just to say
  • 3:54 - 4:03
    like, "Hello." I know. Choo choo. Okay? So
  • 4:03 - 4:05
    this page, I want to see pop-up
  • 4:05 - 4:07
    when I press that button in the browser.
  • 4:07 - 4:08
    Okay. Here we go.
  • 4:08 - 4:11
    So now. What do I need to do? Remember, I
  • 4:11 - 4:13
    need to load the code, I need to load that
  • 4:13 - 4:16
    extension. 05 pop-ups. This is the
  • 4:16 - 4:18
    one I'm gonna select – the whole folder. I'm
  • 4:18 - 4:22
    gonna hit select no errors occurred in
  • 4:22 - 4:24
    the loading of this extension. So it
  • 4:24 - 4:26
    should be fine. Now I can go to any page.
  • 4:26 - 4:26
    I just
  • 4:26 - 4:30
    happen to be going to my Coding Train, I
  • 4:30 - 4:32
    mean, my Chrome extension tutorial page.
  • 4:32 - 4:34
    If l go there now, look. I should be able
  • 4:34 - 4:36
    to – See this button right here? This is my
  • 4:36 - 4:38
    button. The reason why it has that image
  • 4:38 - 4:40
    is because I included the default icon
  • 4:40 - 4:42
    to be this PNG file. And if I click there,
  • 4:42 - 4:51
    look at that! There's my pop-up! It can't
  • 4:51 - 4:53
    have a giant Choo choo right there and not
  • 4:53 - 4:57
    use the train whistle. All right. So, we
  • 4:57 - 4:59
    see that the pop-ups work. Now, let's try
  • 4:59 - 5:00
    to do something a little bit more
  • 5:00 - 5:02
    interesting with the pop-up. Let me see
  • 5:02 - 5:03
    if I can put an animation there or
  • 5:03 - 5:04
    something. Let's see if I can put a whole
  • 5:04 - 5:08
    p5 sketch there. So let's try that. So
  • 5:08 - 5:10
    what I want to do I'm gonna just work
  • 5:10 - 5:12
    with the file system for a second. Like,
  • 5:12 - 5:14
    let me see if I can do – I'm gonna make a
  • 5:14 - 5:17
    folder called sketch. And then what I'm
  • 5:17 - 5:19
    gonna do is I'm going to
  • 5:19 - 5:22
    actually delete pop-up.html. We saw
  • 5:22 - 5:25
    that that worked. I'm going to pause for
  • 5:25 - 5:27
    a second and come back in a second. And
  • 5:27 - 5:30
    I'm back. Now,
  • 5:30 - 5:32
    what I have done in that very long
  • 5:32 - 5:33
    amount of time – way longer than it should have
  • 5:33 - 5:36
    taken me – is I have grabbed one of my
  • 5:36 - 5:37
    previous examples from a coding
  • 5:37 - 5:39
    challenge that generates a maze, and I've
  • 5:39 - 5:41
    put all of the code for that coding
  • 5:41 - 5:44
    challenge in a folder called sketch. So,
  • 5:44 - 5:47
    now what I want is for the pop-up to
  • 5:47 - 5:50
    display that example. Let's just look at
  • 5:50 - 5:52
    that example, what that example does. I've
  • 5:52 - 5:53
    got that example also just running here
  • 5:53 - 5:58
    in the browser. Let me just quickly go to
  • 5:58 - 6:00
    that. So this is the example. It
  • 6:00 - 6:02
    generates a maze using a particular
  • 6:02 - 6:05
    algorithm that I used a long time ago
  • 6:05 - 6:06
    in a challenge. So what I want is when I
  • 6:06 - 6:09
    click this – the files not there anymore – I
  • 6:09 - 6:12
    want to see that maze. Okay. Here we go.
  • 6:12 - 6:13
    We're gonna make this happen.
  • 6:13 - 6:16
    So what do I do? What I have to do is –
  • 6:16 - 6:18
    Uh, this file doesn't exist anymore. I
  • 6:18 - 6:21
    deleted it. What I need to do is, here, the
  • 6:21 - 6:27
    default pop-up should be sketch/
  • 6:27 - 6:31
    index.html. So the question is, is that
  • 6:31 - 6:34
    enough? If I just point to that page
  • 6:34 - 6:38
    that's in the sketch folder, will it work,
  • 6:38 - 6:42
    or do I need to do something more to
  • 6:42 - 6:44
    reference the different JavaScript files
  • 6:44 - 6:45
    and libraries and things that I'm using?
  • 6:45 - 6:49
    Let's find out. And then I'm going to
  • 6:49 - 6:51
    change. Oh, let me just show you that here.
  • 6:51 - 6:54
    We should see – We should say, "generate a
  • 6:54 - 7:00
    maze. That's going to be the message,
  • 7:00 - 7:01
    the little tooltip message when you hover
  • 7:01 - 7:03
    over the button. So I need to go and
  • 7:03 - 7:06
    reload the Chrome extension. And it
  • 7:06 - 7:07
    doesn't actually really matter what page
  • 7:07 - 7:10
    I'm on, but I'm gonna stick on this page. If I
  • 7:10 - 7:13
    hover over here, you should see right? I'm
  • 7:13 - 7:16
    getting that generate a maze message. And
  • 7:16 - 7:20
    then – It worked! So that was actually quite
  • 7:20 - 7:25
    easy. So you can see here I have the maze
  • 7:25 - 7:27
    generating in the pop-up, and
  • 7:27 - 7:28
    interestingly enough, let me press that
  • 7:28 - 7:30
    button. It goes away.
  • 7:30 - 7:33
    Is it gonna pause and continue from
  • 7:33 - 7:34
    where it was before?
  • 7:34 - 7:36
    Is it gonna start over? Had it already
  • 7:36 - 7:37
    been running for a while when I first
  • 7:37 - 7:39
    clicked it? I didn't even notice. Let's look
  • 7:39 - 7:42
    again. Let's see. This is interesting. It
  • 7:42 - 7:44
    started over. So notice this is very
  • 7:44 - 7:48
    important. This is a page that's being
  • 7:48 - 7:51
    loaded fresh each time every time you
  • 7:51 - 7:53
    click. It's not a page that's running and
  • 7:53 - 7:55
    being hidden and then revealed. It's
  • 7:55 - 7:56
    actually a new page that's being loaded.
  • 7:56 - 7:59
    Which brings up another question. How do
  • 7:59 - 8:01
    I debug something that's happening on
  • 8:01 - 8:08
    the pop-up? Is there something here? Nope.
  • 8:08 - 8:11
    Whoops. So let's add a console.log
  • 8:11 - 8:14
    into the pop-up. This is actually all of
  • 8:14 - 8:17
    that code. And I'm just going to say
  • 8:17 - 8:22
    console.log frame count. So this is
  • 8:22 - 8:24
    what I would log typically in a p5
  • 8:24 - 8:26
    sketch if I wanted to just see that it
  • 8:26 - 8:28
    was running. It should say 0 1 2 3 4 5,
  • 8:28 - 8:32
    counting every single frame. So now I'm
  • 8:32 - 8:35
    gonna reload the extension. I'm going to
  • 8:35 - 8:38
    refresh over here, and I'm going to click
  • 8:38 - 8:40
    the button. So where is it logging that?
  • 8:40 - 8:42
    Well, let's look in the JavaScript
  • 8:42 - 8:44
    console. Let's try clicking the button
  • 8:44 - 8:48
    again. It's
  • 8:48 - 8:51
    over my console. Let me try again. No. So,
  • 8:51 - 8:55
    notice how this is not where the – Only
  • 8:55 - 8:59
    the content script is playing a role in
  • 8:59 - 9:02
    the actual content page. This pop-up is
  • 9:02 - 9:04
    just information. So this video is about
  • 9:04 - 9:05
    to end because I'm kind of at the end
  • 9:05 - 9:07
    of it. But I need to do something. What I
  • 9:07 - 9:10
    want to show is how you can have the
  • 9:10 - 9:12
    content script send a message – I'm sorry –
  • 9:12 - 9:13
    the pop-up send a message because
  • 9:13 - 9:14
    ultimately, maybe I want to build an
  • 9:14 - 9:17
    interface there where I, you know, I made
  • 9:17 - 9:19
    a Chrome extension where I doodle on
  • 9:19 - 9:20
    the page. So maybe I can like pick a
  • 9:20 - 9:22
    color or change the line thickness, that
  • 9:22 - 9:23
    type of thing. So I'm gonna get to that
  • 9:23 - 9:25
    in the next video. But I at least want to
  • 9:25 - 9:26
    figure out where I can debug this stuff.
  • 9:26 - 9:29
    If I look, here this is where I would
  • 9:29 - 9:31
    have a console.log for a background
  • 9:31 - 9:33
    script. But I don't have a background
  • 9:33 - 9:35
    script. This is a pop-up. I don't see it
  • 9:35 - 9:37
    there. Where is it? So actually – Thank you
  • 9:37 - 9:39
    to the chat who clued me in on this. I'm
  • 9:39 - 9:42
    going to press this button here, and I'm
  • 9:42 - 9:44
    going to right click. And if I right
  • 9:44 - 9:46
    click, I now see I have this option
  • 9:46 - 9:49
    called inspect. And I can select that, and
  • 9:49 - 9:51
    it's going to bring up another window. I
  • 9:51 - 9:53
    can go over to console. So this is the
  • 9:53 - 9:55
    JavaScript console for the pop-up.
  • 9:55 - 9:57
    There's a JavaScript console for the
  • 9:57 - 10:00
    page – content page – there's a JavaScript
  • 10:00 - 10:02
    console for the background script, and
  • 10:02 - 10:04
    now, there's a JavaScript console for
  • 10:04 - 10:06
    this particular pop-up. Okay. So I've
  • 10:06 - 10:09
    shown you how to add a pop-up. You can do
  • 10:09 - 10:09
    this –
  • 10:09 - 10:10
    anything that you can put on a web page
  • 10:10 - 10:13
    can be in a pop-up. You could start
  • 10:13 - 10:16
    querying API's and drawing maps and
  • 10:16 - 10:18
    doing anything in that pop-up. So what I
  • 10:18 - 10:19
    want to do in the next video is show you
  • 10:19 - 10:23
    how to send a message from the pop-up to
  • 10:23 - 10:27
    the content script and change something
  • 10:27 - 10:29
    in the interface by what you're doing in
  • 10:29 - 10:30
    the pop-up. And I kind of have – I have an
  • 10:30 - 10:35
    idea for that.
Title:
11.5: Chrome Extensions: Pop-ups - Programming with Text
Description:

more » « less
Video Language:
English
Duration:
10:41

English subtitles

Revisions