Migrate the extension to use WebExtension Api
This commit is contained in:
parent
818ab1c99a
commit
46452221b1
|
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<script src="monitor.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="paste-target" contenteditable="true" />
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
})
|
||||
|
|
@ -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)
|
||||
})()
|
||||
|
Before Width: | Height: | Size: 309 B After Width: | Height: | Size: 309 B |
|
Before Width: | Height: | Size: 423 B After Width: | Height: | Size: 423 B |
|
Before Width: | Height: | Size: 610 B After Width: | Height: | Size: 610 B |
62
index.js
62
index.js
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
29
package.json
29
package.json
|
|
@ -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"
|
||||
}]
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
var main = require("../");
|
||||
Loading…
Reference in New Issue