-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
126 lines (117 loc) · 3.44 KB
/
popup.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
var run = function(code, callback) {
chrome.tabs.executeScript({ code: code }, function(results) {
callback(results[0]);
});
}
var message = function(string, target) {
document.getElementById(target || "results").innerText = string;
}
var packageFromText = function(text) {
// Some mappings for my packages
if (/little/i.test(text)) {
return "com.rpetrich.littlebrother";
}
if (/recorder/i.test(text)) {
return "com.booleanmagic.displayrecorder";
}
if (/pane/i.test(text)) {
return "com.rpetrich.videopane";
}
if (/auxo/i.test(text)) {
return "com.a3tweaks.auxo-le";
}
if (/grabby/i.test(text)) {
return "com.rpetrich.grabby";
}
if (/lockdown/i.test(text)) {
return "com.rpetrich.biolockdown";
}
if (/fullforce/i.test(text)) {
return "com.rpetrich.fullforce";
}
if (/haptic/i.test(text)) {
return "hapticpro";
}
if (/wizard/i.test(text)) {
return "org.thebigboss.onehandwizard";
}
// Generic matching, to see if something looks like a package identifier
if (/^[\w.-]+$/.test(text)) {
return text;
}
}
var giftPackage = function(packageText, identifier) {
// Precheck
document.getElementById("identifier").value = identifier;
var package = packageFromText(packageText);
if (!package) {
document.getElementById("package").value = packageText;
document.getElementById("submit").style.display = "block";
message("Fill in package identifier above");
return;
}
document.getElementById("package").value = package;
document.getElementById("submit").style.display = "none";
var url = "https://cydia.saurik.com/connect/products/" + package + "/complimentary";
// Fill the form
var finishedFirst;
chrome.webNavigation.onDOMContentLoaded.addListener(function(details) {
if (!finishedFirst) {
finishedFirst = true;
message("Filling form...");
chrome.tabs.executeScript(details.tabId, { code:
"var f=document.createElement('form');f.method='POST';f.action='complimentary_';" +
"var a=document.createElement('input');a.type='hidden';a.name='account';a.value=" + JSON.stringify([identifier]) + "[0];f.appendChild(a);" +
"document.body.appendChild(f);f.submit();"
}, function() {
});
}
}, {
url: [{
urlEquals: url
}]
});
// Handle form submitted callback
var finishedSecond;
chrome.webNavigation.onDOMContentLoaded.addListener(function(details) {
if (!finishedSecond) {
finishedSecond = true;
message("Fetching results...");
chrome.tabs.executeScript(details.tabId, { code:
"document.querySelector('block').innerText"
}, function(results) {
message(results[0]);
document.getElementById("submit").style.display = "block";
chrome.tabs.remove([details.tabId], function() {
// Goodnight Moon
});
});
}
}, {
url: [{
urlEquals: url + "_"
}]
});
// Create our worker tab
message("Loading form...");
chrome.tabs.create({
url: url,
active: false
}, function(tab) {
});
}
message("Getting data...");
run("document.getSelection().toString()", function(selection) {
run("location.hostname+location.pathname", function(urldata) {
if (urldata == "mail.google.com/mail/u/0/") {
run("(function(e){return e[e.length-1].innerText})(document.querySelectorAll('h2'))", function(subject) {
giftPackage(subject, selection);
});
} else {
giftPackage("", selection);
}
});
});
document.getElementById("submit").addEventListener("click", function() {
giftPackage(document.getElementById("package").value, document.getElementById("identifier").value);
}, false);