This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
118 lines (112 loc) · 4.26 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
// URLDefenseDecoder - Chrome Extension right-click URLDefense decoder
// Copyright (C) 2017 FractalSystems.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public LICENSE
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// https://www.gnu.org/licenses/gpl-3.0.en.html
// A generic onclick callback function.
function genericOnClick(info) {}
function copyToClipboard(text) {
var copyDiv = document.createElement('div');
copyDiv.contentEditable = true;
document.body.appendChild(copyDiv);
copyDiv.innerHTML = text;
copyDiv.unselectable = "off";
copyDiv.focus();
document.execCommand('SelectAll');
document.execCommand("Copy", false, null);
document.body.removeChild(copyDiv);
}
var contexts = ["link", "selection"];
var title = "Copy URLDefense-Decoded-Link";
for (var i = 0; i < contexts.length; i++) {
var context = contexts[i];
console.log(context);
if (context === "link") {
chrome.contextMenus.onClicked.addListener(onClickHandler);
function onClickHandler(info) {
var lText = info.linkUrl;
if (/https:\/\/urldefense.proofpoint.com\/v1/.test(lText)) {
var reg = /u=(.+?)&k=/;
var specialencodedurl = lText.match(reg);
var transurl = specialencodedurl[1];
var decodedurl = decodeURIComponent(transurl);
var url = decodedurl;
copyToClipboard(decodedurl)
} else if (/https:\/\/urldefense.proofpoint.com\/v2/.test(lText)) {
var reg = /u=(.+?)&[dc]=/;
var specialencodedurl = lText.match(reg);
var transurl = specialencodedurl[1]
var urlencodedurl = transurl.replace(/-/g, '%');
var htmlencodedurl = urlencodedurl.replace(/_/g, '/');
var decodedurl = decodeURIComponent(htmlencodedurl);
var url = decodedurl;
copyToClipboard(decodedurl)
}
};
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({
"title": title,
"id": "linkid",
"contexts": ["link"],
"onclick": genericOnClick,
"targetUrlPatterns": ["https://urldefense.proofpoint.com/*"]
});
});
} else if (context === "selection") {
chrome.contextMenus.onClicked.addListener(onClickHandler);
function onClickHandler(info) {
var sText = info.selectionText;
if (/https:\/\/urldefense.proofpoint.com\/v1/.test(sText)) {
var reg = /u=(.+?)&k=/;
var specialencodedurl = sText.match(reg);
var transurl = specialencodedurl[1];
var decodedurl = decodeURIComponent(transurl);
var url = decodedurl;
copyToClipboard(decodedurl)
} else if (/https:\/\/urldefense.proofpoint.com\/v2/.test(sText)) {
var reg = /u=(.+?)&[dc]=/;
var specialencodedurl = sText.match(reg);
var transurl = specialencodedurl[1]
var urlencodedurl = transurl.replace(/-/g, '%');
var htmlencodedurl = urlencodedurl.replace(/_/g, '/');
var decodedurl = decodeURIComponent(htmlencodedurl);
var url = decodedurl;
copyToClipboard(decodedurl)
}
};
var selectionid;
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.request === 'updateContextMenu') {
var type = msg.selection;
if (/https:\/\/urldefense.proofpoint.com/.test(type)) {
var options = {
title: title,
contexts: ["selection"],
onclick: genericOnClick
};
if (selectionid != null) {
chrome.contextMenus.update(selectionid, options);
} else {
selectionid = chrome.contextMenus.create(options);
}
} else {
if (selectionid != null) {
chrome.contextMenus.remove(selectionid);
selectionid = null;
}
}
}
});
}
}