-
Notifications
You must be signed in to change notification settings - Fork 27
/
panel.js
171 lines (143 loc) · 5.92 KB
/
panel.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
console.log("paneljs loaded");
/*
var test = chrome.devtools.inspectedWindow.eval(
"jQuery.fn.jquery",
function(result, isException) {
if (isException){
console.log("the page is not using jQuery");
return false;
}
else{
console.log("The page is using jQuery v" + result);
return true;
}
}
);
console.log("test:",test);
*/
function safe_tags(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
function getUrlVars(url) {
console.log("getting vars");
var vars = [],
hash;
var hashes = url.slice(url.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
/*
chrome.permissions.contains({
permissions: ['tabs'],
origins: ['<all_urls>']
}, function(result) {
if (result) {
$("#load").remove();
console.log("Perm granted");
// inject hsinspector
chrome.tabs.executeScript(chrome.devtools.inspectedWindow.tabId, {
file: "hsInspector.js"
});
$(".explanation").remove();
} else {
// The extension doesn't have the permissions.
}
});
document.querySelector('#load').addEventListener('click', function(event) {
// Permissions must be requested from inside a user gesture, like a button's
// click handler - clicking the button and accepting grants permission.
chrome.permissions.request({
permissions: ['tabs'],
origins: ['<all_urls>']
}, function(granted) {
// The callback argument will be true if the user granted the permissions.
if (granted) {
console.log("Perm granted");
//inject hsinspector
chrome.tabs.executeScript(chrome.devtools.inspectedWindow.tabId, {
file: "hsInspector.js"
});
$(".explanation").remove();
} else {
console.log("Perm denied");
//should display a message - cannot show dev info without permission
}
});
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.devInfoURL) {
sendResponse({
farewell: "devInfoURL recieved."
});
//display dev info link from menu
$("h1").text(request.devInfoURL);
//$("#dummy").attr('src', request.devInfoURL);
console.log(getUrlVars(request.devInfoURL));
var devInfoData = getUrlVars(request.devInfoURL);
console.log("PORTAL ID:", devInfoData.portalId);
var portalId = devInfoData.portalId;
console.log(sender.tab.url);
const inspectedURL = new URL(sender.tab.url);
console.log(inspectedURL.hostname);
console.log(inspectedURL.pathname);
console.log("getting Token");
jQuery.ajax({
type: 'GET',
url: 'https://login.hubspot.com/login/api-verify',
data: {
'portalId': portalId
}, // change to grab token
xhrFields: {
withCredentials: true
}
}).done(function(loginData) {
console.log("result:", loginData);
var currentToken = loginData.auth.access_token.token;
console.log("token", currentToken);
var accessToken = currentToken; //doing this to pass var to HS code without touching their code to reduce screwing it up
//now prep the data to make the ajax call to get the true dev info URL
var envSuffix = (window.location.hostname.indexOf('qa') !== -1) ? 'qa' : ''; // checks if HS QA site
console.log("envSuffix", envSuffix);
//code below was pulled right off of HS's dev info redirection page
var url = devInfoData.url,
portalId = devInfoData.portalId,
loginApiHost = 'login.hubspot' + envSuffix + '.com',
apiHost = 'api.hubapi' + envSuffix + '.com',
parser = document.createElement('a'); //not sure what in the world this is for, maybe incase auto redirect fails?
parser.href = url;
var accessToken = currentToken;
var currentDomain = inspectedURL.hostname;
$.ajax({
//url: 'https://' + apiHost + '/content/api/v4/domains/by-domain?portalId=' + portalId + '&domain=' + parser.hostname + '&access_token=' + accessToken,
url: 'https://' + currentDomain + '/__context__/' + inspectedURL.pathname + '?portalId=' + portalId + '&access_token=' + accessToken,
xhrFields: {
withCredentials: true
}
}).done(function(result) {
console.log(result);
$("body").prepend($("<div id='dev-info'>" + safe_tags(JSON.stringify(result, undefined, 2)) + "</div>"))
formatJSON();
// TODO we should be able to load the css a different way in panels, we'll need to look into that
$.get(chrome.extension.getURL('/json-formatter.css'), function(data) {
$("body").prepend("<style id='json-formatter-css'>" + data + "</style>");
});
});
})
}
}
);
*/