-
Notifications
You must be signed in to change notification settings - Fork 6
/
content.js
90 lines (78 loc) · 2.39 KB
/
content.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
chrome.runtime.sendMessage({ todo: "showPageAction" });
var realContent = [];
var parArray;
var flag = 0;
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.todo == "ceasarEncrypt") {
pArray = document.getElementsByTagName("p");
for (var i = 0; i < pArray.length; i++) {
var text = document.getElementsByTagName("p")[i].innerText;
if (realContent.length < pArray.length && flag === 0) {
realContent.push(text);
}
var encryptText = ceaserencrypt(text, 5);
document.getElementsByTagName("p")[i].innerText = encryptText;
}
flag = 1;
}
if (request.todo == "base64") {
pArray = document.getElementsByTagName("p");
for (var i = 0; i < pArray.length; i++) {
var text = document.getElementsByTagName("p")[i].innerText;
if (realContent.length < pArray.length && flag === 0) {
realContent.push(text);
}
var encryptText = base64(text);
document.getElementsByTagName("p")[i].innerText = encryptText;
}
flag = 1;
}
if (request.todo == "Rot13") {
pArray = document.getElementsByTagName("p");
for (var i = 0; i < pArray.length; i++) {
var text = document.getElementsByTagName("p")[i].innerText;
if (realContent.length < pArray.length && flag === 0) {
realContent.push(text);
}
var encryptText = rot13(text);
document.getElementsByTagName("p")[i].innerText = encryptText;
}
flag = 1;
}
if (request.todo == "reset") {
for (var i = 0; i < realContent.length; i++) {
var text = realContent[i];
document.getElementsByTagName("p")[i].innerText = text;
}
}
});
function ceaserencrypt(msg, key) {
var encMsg = "";
for (var i = 0; i < msg.length; i++) {
var code = msg.charCodeAt(i);
// Encrypt only letters in 'A' ... 'Z' interval
if (code >= 65 && code <= 65 + 26 - 1) {
code -= 65;
code = (code + key) % 26;
code += 65;
} else if (code >= 97 && code <= 97 + 26 - 1) {
code -= 97;
code = (code + key) % 26;
code += 97;
}
encMsg += String.fromCharCode(code);
}
return encMsg;
}
function base64(msg) {
return btoa(msg);
}
function rot13(msg) {
var encMsg = msg;
encMsg = encMsg.replace(/[a-zA-Z]/g, function (c) {
return String.fromCharCode(
(c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26
);
});
return encMsg;
}