-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (62 loc) · 2.31 KB
/
script.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
function inCanonicalBlocklist (url) {
// some website does not provide the right canonical url, we need to ignore them.
// https://en.wikipedia.org/wiki/Canonical_link_element
var blocklist = [
/^https:\/\/mail.google.com/i,
/^https:\/\/www.draw.io/i,
/^https:\/\/www.goodreads.com/i,
/^https:\/\/www.tw.kayak.com/i,
/^https:\/\/www.fedex.com/i,
/^https:\/\/forum.snapcraft.io/i,
/^https:\/\/store.neosmartpen.com/i,
/^https:\/\/bugs.debian.org/i
];
var ret = false;
blocklist.forEach(function(pattern, index) {
if(pattern.test(url)) {
ret = true;
}
});
return ret;
}
function hexdump(buffer, blockSize) {
blockSize = blockSize || 16;
var lines = [];
var hex = "0123456789ABCDEF";
for (var b = 0; b < buffer.length; b += blockSize) {
var block = buffer.slice(b, Math.min(b + blockSize, buffer.length));
var addr = ("0000" + b.toString(16)).slice(-4);
var codes = block.split('').map(function (ch) {
var code = ch.charCodeAt(0);
return " " + hex[(0xF0 & code) >> 4] + hex[0x0F & code];
}).join("");
codes += " ".repeat(blockSize - block.length);
var chars = block.replace(/[\x00-\x1F\x20]/g, '.');
chars += " ".repeat(blockSize - block.length);
lines.push(addr + " " + codes + " " + chars);
}
return lines.join("\n");
}
setTimeout(function(){
var linkdata = {};
linkdata.title = window.document.title;
linkdata.text = window.document.title;
linkdata.url = window.location.href;
var sel = window.getSelection().toString();
if(sel != '')
linkdata.text = sel;
// clean line break at the end of string.
linkdata.text = linkdata.text.replace(/[\r\n]+$/, '');
if(!inCanonicalBlocklist(linkdata.url)) {
var canonical = window.document.querySelector('link[rel=canonical],link[rel=shorturl],link[rel=shortlink]');
if (canonical)
linkdata.url = canonical.href;
}
chrome.runtime.sendMessage({action: "copyurl", linkdata: linkdata},
function(response) {
var lastError = chrome.runtime.lastError;
if (lastError) {
console.log(lastError.message);
}
});
},100);