Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a Workable Chrome Extension in React &...

Building a Workable Chrome Extension in React & Redux

In this talk, we demonstrate a message passing paradigm between the different building blocks of a Chrome Extension (i.e. content scripts and background script) based on Redux principles and accessories. Furthermore, we achieve content script encapsulation by leveraging React componentization and Shadow DOM.

Project companion: https://github.com/Workable/devit2016

Sokratis Vidros

May 20, 2016
Tweet

More Decks by Sokratis Vidros

Other Decks in Programming

Transcript

  1. We are... Sokratis Vidros Senior Software Engineer @ Workable sokratisvidros

    Nikos Kostoulas Senior Software Engineer @ Workable nkostoulas
  2. 1. Select a GoT character name in narratives 2. View

    their spoiler on the side Scenario
  3. Background page • Fetch spoiler from server • Memoize last

    spoiler Responsibilities Content script • Interact with page DOM • Display the spoiler on the side
  4. Background page Flow Content script 1. Select name in text

    2. Send it to the background 6.Render the spoiler 5. Pass it to the content script 3. Receive the selected name 4. Fetch spoiler from server
  5. We would also like to... • Render data efficiently •

    Achieve content script isolation • Facilitate message passing • Add memory
  6. { tab, foreground: true, type: ... } Content script Background

    script Architecture View Action Middleware Reducers Store Action Middleware Reducers Store { payload: … } { background: true, type: ... }
  7. Step 1: Scaffolding • Add manifest.json • Add the background

    Redux Stack • Add the content script React/Redux Stack • Set-up reduxChromeGofer middleware > git checkout step1
  8. Step 2: Content script encapsulation • Leverage React componentization ◦

    Root container component ◦ Spoiler presentational component • Enclose root component in a shadow root ◦ Style encapsulation and isolation > git checkout step2
  9. Step 3: Request spoiler Background Script Content Script { background:

    true, type: “REQUEST_SPOILER”, name: ... } • Transfer action from content to background • Execute ajax request from the background ◦ No same-origin policy limitations > git checkout step3 GET /spoilers.json?c=eddard Select name
  10. Content Script Background Script Step 4: Receive spoiler dispatch(receiveSpoiler()) {

    foreground: true, tab, type: “RECEIVE_SPOILER”, spoiler: ... } • Dispatch receive action upon retrieval • Pass the spoiler to content script > git checkout step4
  11. • Listen to background script state changes • Push background

    state to every content script Step 5: Sync state Background Script Content Script Content Script Content Script > git checkout step5
  12. ✓ Back end engineers ✓ Front end engineers ✓ Machine

    learning engineers ✓ Data scientists ✓ DevOps ✓ UI / UX designers We’re constantly looking for: Apply at careers.workable.com