-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
309 lines (272 loc) · 9.68 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2012 wtwf.com, All Rights Reserved.
// Created by: Alex K (wtwf.com)
/* global chrome, localStorage */
// TODO allow updating of patterns to batter ones.
// TODO we can expand short urls too!?
// TODO have a toolbar icon to turn this on and off
// TODO allow credit card looking numbers only for a short period of time?
// TODO allow you to send clipboard contents to other machines logged in with this user.
// NOTODO should we do http://amzn.com/ASIN ? not until it supports https
// The icons are creative commons (non-commercial) licensed from:
// http://www.ilmarin.info/gallery/album128/scissors01
var copyclipper = {} // Namespace
/** search and replace regexes
* !search!replace!regex_options
* remember you can use $1, $2 etc in the replace string
*/
copyclipper.DEFAULT_REGEXES = [
'# Add all new patterns above this line.',
'# Begin Default Patterns',
'# If you don\'t want one of thise patterns to execute, add a # at the start',
'!\\?(fbclid|ref|authType)=.*!!',
'!\\?filter=following.*!!',
'!\\?fromListing=listing!!',
'!\\?from_search=.*!!',
'!\\?mt=[0-9]+!!',
'!^(http.*)&referer=brief_results$!$1!',
'!^(http.*)\\?_r=1$!$1!', // nytimes
'!^(http.*)\\?feat=directlink$!$1!', // picasaweb
'!^(http.*)\\?lang=en$!$1!',
'!^(http.*)\\?source=rss$!$1!', // sentinel
'!^(http.*)\\?utm_source=[^#]*!$1!', // GA
'!^(http.*?)[&?]utm_(source|medium|campaign)=[^#]*!$1!',
'!^(http.*imdb.com.*?)/?\\?.*!$1!',
'!^(https://www.aliexpress.com/item).*(/\\d+\\.html)\\?.*!$1$2!',
'!^(https://www.ebay.com/itm).*(/\\d+)\\?.*!$1/_$2!',
'!^(https://www.homedepot.com/p).*(/\\d+)(\\?.*)?!$1/_$2!',
'!^http.*google.*search.*[?&]tbm=isch&q=([^&]*).*$!https://www.google.com/search?tbm=isch&q=$1!',
'!^http.*youtube.*[&?]v=([^&]*).*?(#t=.*)?$!https://youtu.be/$1$2!',
'!^https://code.google.com/p/chromium/issues/detail\\?([^&]*&)*id=(\\d+).*$!http://crbug.com/$2!',
'!^https?://([a-z0-9.]*amazon[^/]*/(gp/product|([^/]*/)?[dg]p)/[^/?]*).*$!https://$1!',
'!^https://[^/]*\\.google\\.[^/]*/url\\?q=([^&]*).*!$1!u',
'# Uncomment this next line (remove the #) if you feel brave!',
'#!/index\\.(php|s?html?|asp)$!/!',
'# End Default Patterns'
]
/** How long to wait before the Chrome notification disappears, in milliseconds. */
copyclipper.NOTIFICATION_TIMEOUT = 8000
/** Is an object empty.
* @param {Object} obj the object to check.
* @return {bool} true if object has properties.
*/
copyclipper.isEmpty = function (obj) {
for (var i in obj) {
if (Object.prototype.hasOwnProperty.call(obj, i)) {
return false
}
}
return true
}
copyclipper.restoreRegexes = function () {
// try and get the data from chrome.storage.sync
chrome.storage.sync.get('regexes', function (reply) {
var regexes
if (copyclipper.isEmpty(reply)) {
// maybe they are in localStorage?
if (Object.prototype.hasOwnProperty.call(localStorage, 'copyclipper.regexes') &&
localStorage['copyclipper.regexes']) {
console.log('loaded regexes from localStorage')
regexes = localStorage['copyclipper.regexes']
} else {
console.log('no regexes found, using defaults')
regexes = copyclipper.DEFAULT_REGEXES.join('\n')
}
chrome.storage.sync.set({ regexes: regexes }, function () {
console.log('regexes saved to chrome.storage.sync')
})
} else {
console.log('loaded regexes from chrome.storage.sync')
regexes = reply.regexes
}
copyclipper.patterns = copyclipper.decodeRegexes(regexes)
})
}
copyclipper.decodeRegexes = function (regexBlob) {
var regexLines = regexBlob.split('\n')
var patterns = []
for (var i = 0; i < regexLines.length; i++) {
var pattern = copyclipper.decodeRegexLine(regexLines[i])
if (pattern) {
patterns.push(pattern)
}
}
return patterns
}
/** Remove whitespace from the start & end of the line. */
copyclipper.trim = function (s) {
return s.replace(/^\s+/, '').replace(/\s+$/, '')
}
/** Take a line like !search!replace!re_args and turn it into a
* Two element [RegExp, string] Array.
* Returns null on comment lines or an error parsing.
*/
copyclipper.decodeRegexLine = function (regexLine) {
console.log(regexLine)
regexLine = copyclipper.trim(regexLine)
if (regexLine.charAt(0) === '#') {
// ignore comment lines
return null
}
var oldParts = regexLine.split('!')
var pos = 1
var parts = []
while (pos < oldParts.length) {
parts.push(oldParts[pos++])
while (oldParts[pos - 1].charAt(oldParts[pos - 1].length - 1) === '\\' &&
pos < oldParts.length) {
parts[parts.length - 1] = parts[parts.length - 1] + '!' + oldParts[pos]
pos++
}
}
if (parts.length === 3) {
var newopts = parts[2].replace('u', '')
var options = {}
if (newopts !== parts[2]) {
options.urlDecode = true
}
return [RegExp(parts[0], newopts), parts[1], options]
}
return null
}
copyclipper.textToHtml = function (txt) {
txt = txt.replace('&', '&')
txt = txt.replace('<', '<')
txt = txt.replace('>', '>')
return txt
}
copyclipper.getClipboardContents = function () {
var el = document.getElementById('txt')
el.focus()
el.select()
document.execCommand('paste')
return el.value
}
copyclipper.setClipboardContents = function (val) {
var el = document.getElementById('txt')
el.value = val
el.focus()
el.select()
document.execCommand('copy')
copyclipper.clipboardValue = val
}
/** Go through all our patterns and search/replace on val. */
copyclipper.replace = function (val, patterns) {
if (patterns && patterns.length) {
for (var i = 0; i < patterns.length; i++) {
var search = patterns[i][0]
var replace = patterns[i][1]
var { urlDecode } = patterns[i][2] || {}
var newval = val.replace(search, replace)
if (newval !== val && urlDecode && newval.search(/\?[^=%]+%3D/)) {
newval = copyclipper.replace(decodeURIComponent(newval))
}
val = newval
}
}
return val
}
/** Called when the clipboard has changed, see if we can clip the clipboard. */
copyclipper.copyclip = function () {
var val = copyclipper.getClipboardContents()
var orig = val + ''
val = copyclipper.replace(val, copyclipper.patterns)
copyclipper.clipboardValue = val
copyclipper.clipboardValueOriginal = orig
if (val !== orig) {
copyclipper.setClipboardContents(val)
copyclipper.createNotification()
}
}
/** Called every second to see if the clipboard has changed. */
copyclipper.pollClipboard = function () {
var val = copyclipper.getClipboardContents()
if (val !== copyclipper.clipboardValue) {
copyclipper.copyclip()
}
}
copyclipper.restoreRegexes()
copyclipper.clipboardValue = '' // the last value we saw
copyclipper.clipboardValueOriginal = ''
/*
navigator.permissions.query({
name: 'clipboard-read'
}).then(permissionStatus => {
// Will be 'granted', 'denied' or 'prompt':
console.log(permissionStatus.state);
// Listen for changes to the permission state
permissionStatus.onchange = () => {
console.log(permissionStatus.state);
};
});
*/
function getChromeVersion () {
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)
return raw ? parseInt(raw[2], 10) : false
}
if (getChromeVersion() > 69 && ((chrome.clipboard || {}).onClipboardDataChanged || {}).addEventListener) {
console.log('FINALLY USING NEW onClipboardDataChanged API!')
// use new api https://developer.chrome.com/apps/clipboard
chrome.clipboard.onClipboardDataChanged.addListener(copyclipper.pollClipboard)
} else {
// Check the clipboard every second, if anything changed copyclip/filter it.
copyclipper.intervalId = window.setInterval(copyclipper.pollClipboard, 1000)
}
copyclipper.notificationTimer = null
copyclipper.createNotification = function () {
var desciption = '\n\nClipped from:\n'
if (copyclipper.clipboardValueOriginal.length <
copyclipper.clipboardValue.length) {
desciption = '\n\nUnclipped from:\n'
}
const message = (copyclipper.clipboardValue + desciption + copyclipper.clipboardValueOriginal)
const buttons = [{ title: 'Undo' }]
copyclipper.showNotification(message, buttons)
}
copyclipper.showNotification = function (message, buttons) {
copyclipper.clearNotifications()
chrome.notifications.create('',
{
type: 'basic',
title: 'Copyclipper',
message: message,
iconUrl: 'icon48.png',
buttons: buttons
},
function (notificationId) {})
if (copyclipper.notificationTimer != null) {
clearTimeout(copyclipper.notificationTimer)
}
copyclipper.notificationTimer = setTimeout(copyclipper.clearNotifications,
copyclipper.NOTIFICATION_TIMEOUT)
}
copyclipper.clearNotifications = function () {
chrome.notifications.getAll(function (notificationIds) {
for (var notificationId in notificationIds) {
chrome.notifications.clear(notificationId, function (wasCleared) {})
}
})
}
copyclipper.notificationButtonClicked = function (notificationId, buttonIndex) {
var orig = copyclipper.clipboardValueOriginal
copyclipper.clipboardValueOriginal = copyclipper.clipboardValue
copyclipper.setClipboardContents(orig)
copyclipper.createNotification()
}
copyclipper.remoteSetClipboard = function (newValue, from) {
copyclipper.clipboardValueOriginal = copyclipper.clipboardValue = newValue
copyclipper.setClipboardContents(newValue)
let message = 'Received clipboard'
if (from) {
message = message + ' from ' + from
}
copyclipper.showNotification(message, [])
}
// handlers for notification clicks
chrome.notifications.onButtonClicked.addListener(
copyclipper.notificationButtonClicked)
chrome.storage.onChanged.addListener(function (changed, area) {
if (area === 'sync' && changed.regexes) {
console.log('regexes changed')
copyclipper.restoreRegexes()
}
})