Skip to content

Instantly share code, notes, and snippets.

@felixr
Last active January 1, 2021 16:15
Show Gist options
  • Select an option

  • Save felixr/dec17c245e2c1611f6163d04a25caa19 to your computer and use it in GitHub Desktop.

Select an option

Save felixr/dec17c245e2c1611f6163d04a25caa19 to your computer and use it in GitHub Desktop.

Revisions

  1. felixr revised this gist Jan 1, 2021. No changes.
  2. felixr renamed this gist Jan 1, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. felixr created this gist Jan 1, 2021.
    42 changes: 42 additions & 0 deletions background.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@

    function handleEvent(name, payload) {
    fetch('http://localhost:8118/report?'+JSON.stringify({name, payload}));
    }

    chrome.tabs.onActivated.addListener(
    (tabId, windowId) => handleEvent("activated", {tabId, windowId}));

    chrome.tabs.onActiveChanged.addListener(
    (tabId, selectInfo) => handleEvent("activeChanged", {tabId, selectInfo}));

    chrome.tabs.onAttached.addListener(
    (tabId, attachInfo) => handleEvent("attached", {tabId, attachInfo}));

    chrome.tabs.onDetached.addListener(
    (tabId, detachInfo) => handleEvent("detached", {tabId, detachInfo}));

    chrome.tabs.onCreated.addListener((tab) => handleEvent("created", {tab}));

    chrome.tabs.onHighlightChanged.addListener(
    (selectInfo) => handleEvent("highlightChanged", {selectInfo}));

    chrome.tabs.onHighlighted.addListener(
    (hightlighInfo) => handleEvent("hightlighted", {hightlighInfo}));

    chrome.tabs.onMoved.addListener(
    (tabId, moveInfo) => handleEvent("moved", {tabId, moveInfo}));

    chrome.tabs.onRemoved.addListener(
    (tabId, removeInfo) => handleEvent("removed", {tabId, removeInfo}));

    chrome.tabs.onReplaced.addListener(
    (addedTabId, removedTabId) => handleEvent("replaced", {addedTabId, removedTabId}));

    chrome.tabs.onReplaced.addListener(
    (addedTabId, removedTabId) => handleEvent("replaced", {addedTabId, removedTabId}));

    chrome.tabs.onSelectionChanged.addListener(
    (tabId, selectInfo) => handleEvent("selectionChanged", {tabId, selectInfo}));

    chrome.tabs.onUpdated.addListener(
    (tabId, changeInfo, tab) => handleEvent("updated", {tabId, changeInfo, tab}));
    11 changes: 11 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    {
    "name": "Tab reporter",
    "description": "reports tab events",
    "version": "0.1",
    "permissions": ["tabs", "http://localhost:8118/*"],
    "background": {
    "persistent": false,
    "scripts": ["background.js"]
    },
    "manifest_version": 2
    }
    2 changes: 2 additions & 0 deletions run_server.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # listen to events
    janet -e '(import json) (net/server "0.0.0.0" 8118 (fn [conn] (as-> (net/read conn 4096) _ (string/split " " _) (_ 1) (string/slice _ 8) (string/replace-all "%22" "\"" _) (json/decode _) (pp _)) (net/close conn)))'