From 46452221b1a9a66eca0545d97c239340aca3f3ed Mon Sep 17 00:00:00 2001 From: Kamil Tomala Date: Fri, 11 Aug 2017 17:41:11 +0200 Subject: [PATCH] Migrate the extension to use WebExtension Api --- bg/index.html | 8 +++++ bg/monitor.js | 61 +++++++++++++++++++++++++++++++++++++ data/inserter.js | 6 ---- fg/insert.js | 16 ++++++++++ {data => icon}/icon16.png | Bin {data => icon}/icon32.png | Bin {data => icon}/icon64.png | Bin index.js | 62 -------------------------------------- manifest.json | 28 +++++++++++++++++ package.json | 29 ------------------ test/test-index.js | 1 - 11 files changed, 113 insertions(+), 98 deletions(-) create mode 100644 bg/index.html create mode 100644 bg/monitor.js delete mode 100644 data/inserter.js create mode 100644 fg/insert.js rename {data => icon}/icon16.png (100%) rename {data => icon}/icon32.png (100%) rename {data => icon}/icon64.png (100%) delete mode 100644 index.js create mode 100644 manifest.json delete mode 100644 package.json delete mode 100644 test/test-index.js diff --git a/bg/index.html b/bg/index.html new file mode 100644 index 0000000..9b23e9a --- /dev/null +++ b/bg/index.html @@ -0,0 +1,8 @@ + + + + + +
+ + diff --git a/bg/monitor.js b/bg/monitor.js new file mode 100644 index 0000000..7d0664a --- /dev/null +++ b/bg/monitor.js @@ -0,0 +1,61 @@ +console.log("I'm alive") + +let previousContent = "" +let listeningTabs = [] +let timer = null + +browser.browserAction.onClicked.addListener(() => { + browser.tabs.query({active: true}) + .then(([t]) => toggleTab(t.id)) +}) + +function toggleTab(id) { + const index = listeningTabs.indexOf(id) + if(index >= 0) { + uninject(id) + listeningTabs.splice(index, 1) + updateTimer() + browser.browserAction.setBadgeText({ text: "", tabId: id }) + } else { + browser.tabs.executeScript({file: "/fg/insert.js"}) + listeningTabs.push(id) + updateTimer() + browser.browserAction.setBadgeText({ text: "ON", tabId: id }) + browser.browserAction.setBadgeBackgroundColor({ color: "green", tabId: id }) + } +} + +function notifyForeground(id, text) { + browser.tabs.sendMessage(id, { + action: "insert", text + }) +} + +function uninject(id) { + browser.tabs.sendMessage(id, { action: "uninject" }) +} + +function checkClipboard() { + const pasteTarget = document.querySelector("#paste-target") + pasteTarget.textContent = "" + pasteTarget.focus() + document.execCommand("paste") + const content = pasteTarget.textContent + if(content != previousContent) { + listeningTabs.forEach(id => notifyForeground(id, content)) + previousContent = content + } +} + +function updateTimer() { + if(listeningTabs.length > 0) { + if(timer === null) { + const id = setInterval(checkClipboard, 1000) + timer = { id } + } + } else { + if(timer !== null) { + clearInterval(timer.id) + } + } +} diff --git a/data/inserter.js b/data/inserter.js deleted file mode 100644 index aba6ab3..0000000 --- a/data/inserter.js +++ /dev/null @@ -1,6 +0,0 @@ -self.port.on('insert', (data, prefs) => { - var elem = document.createElement(prefs['element-name']) - elem.textContent = data - var container = document.querySelector(prefs['container-selector']) - document.body.appendChild(elem) -}) diff --git a/fg/insert.js b/fg/insert.js new file mode 100644 index 0000000..7f20e18 --- /dev/null +++ b/fg/insert.js @@ -0,0 +1,16 @@ +(() => { + const processMessage = msg => { + switch(msg.action) { + case "insert": + const elem = document.createElement('p') + elem.textContent = msg.text + document.querySelector("body").appendChild(elem) + break + case "uninject": + browser.runtime.onMessage.removeListener(processMessage) + break + } + } + + browser.runtime.onMessage.addListener(processMessage) +})() diff --git a/data/icon16.png b/icon/icon16.png similarity index 100% rename from data/icon16.png rename to icon/icon16.png diff --git a/data/icon32.png b/icon/icon32.png similarity index 100% rename from data/icon32.png rename to icon/icon32.png diff --git a/data/icon64.png b/icon/icon64.png similarity index 100% rename from data/icon64.png rename to icon/icon64.png diff --git a/index.js b/index.js deleted file mode 100644 index 9cb5c6f..0000000 --- a/index.js +++ /dev/null @@ -1,62 +0,0 @@ -var self = require('sdk/self'); -var clipboard = require('sdk/clipboard') -var buttons = require('sdk/ui/button/action'); -var tabs = require('sdk/tabs'); -var { ToggleButton } = require('sdk/ui/button/toggle') -var timers = require('sdk/timers') -var { prefs } = require('sdk/simple-prefs') - -var button = ToggleButton({ - id: "clipboard-inserter-btn", - label: "Toggle Clipboard Cnserter", - icon: { - 16: "./icon16.png", - 32: "./icon32.png", - 64: "./icon64.png" - }, - onChange: function(state) { - this.state('window', null) - let { checked } = this.state('tab') - checked = !checked - this.state('tab', { checked }) - if(checked) { - enableMonitor() - } else { - disableMonitor() - } - } -}) - -function enableMonitor() { - console.log("Enabling clipboard monitor") - tabs.activeTab.clinClipboardMonitor = new ClipboardMonitor() -} - -function disableMonitor() { - console.log("Disabling clipboard monitor") - var monitor = tabs.activeTab.clinClipboardMonitor - if(monitor) { - timers.clearInterval(monitor.intervalID) - delete tabs.activeTab.clinClipboardMonitor - } else { - console.error("Trying to disable clipboard monitor on tab without active ClipboardMonitor!") - } -} - -function ClipboardMonitor() { - - var lastContent = '' - - this.worker = tabs.activeTab.attach({ contentScriptFile: self.data.url('inserter.js') }) - - this.intervalID = timers.setInterval(() => { - if(clipboard.currentFlavors.indexOf('text') != -1) { - var currentContent = clipboard.get('text/unicode') - if(lastContent !== currentContent) { - this.worker.port.emit('insert', currentContent, prefs) - lastContent = currentContent - } - } - }, 100) - -} diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..0a8c8ad --- /dev/null +++ b/manifest.json @@ -0,0 +1,28 @@ +{ + "manifest_version": 2, + "name": "Clipboard Inserter", + "version": "0.2.0", + + "description": "A simple addon whose purpose is to automatically insert contents of clipboard into the page. Uses icon made by Google from www.flaticon.com licensed by CC 3.0 BY", + + "icons": {}, + + "permissions": [ + "activeTab", + "clipboardRead" + ], + + "browser_action": { + "browser_style": true, + "default_icon": { + "16": "icon/icon16.png", + "32": "icon/icon32.png", + "64": "icon/icon64.png" + }, + "default_title": "Toggle clipboard inserter" + }, + + "background": { + "page": "bg/index.html" + } +} diff --git a/package.json b/package.json deleted file mode 100644 index 9e2dcd2..0000000 --- a/package.json +++ /dev/null @@ -1,29 +0,0 @@ - -{ - "title": "Clipboard Inserter", - "name": "clipboard-inserter", - "version": "0.1.0", - "description": "A simple addon whose purpose is to automatically insert contents of clipboard into the page. Uses icon made by Google from www.flaticon.com licensed by CC 3.0 BY", - "main": "index.js", - "author": "Yoitsumi", - "engines": { - "firefox": ">=38.0a1", - "fennec": ">=38.0a1" - }, - "license": "MIT", - "keywords": [ - "jetpack" - ], - "preferences": [{ - "name": "element-name", - "title": "Added element name", - "type": "string", - "value": "p" - }, { - "name": "container-selector", - "title": "Containing element selector", - "description": "css selector of the element which should contain added elements. If you don't know what this means leaving this as 'body' will just append at the end of the document", - "type": "string", - "value": "body" - }] -} diff --git a/test/test-index.js b/test/test-index.js deleted file mode 100644 index 34c319b..0000000 --- a/test/test-index.js +++ /dev/null @@ -1 +0,0 @@ -var main = require("../");