-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpopup.js
369 lines (329 loc) · 16.5 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*Google Analytics*/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-122315369-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
/*end google analytics*/
function trackClick(eventName){
chrome.runtime.sendMessage({trackClick: eventName}, function(response) {
console.log(response.farewell);
});
}
var developerTools = {
debugReload: function(debugParam) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var tabUrl = new URL(tabs[0].url);
var params = new URLSearchParams(tabUrl.search);
trackClick(debugParam);
if (debugParam === "hsCacheBuster") {
var randomNum = Math.floor(Math.random() * 9999) + 1;
params.set("hsCacheBuster", randomNum);
} else if (params.has(debugParam)) {
params.delete(debugParam);
} else {
params.append(debugParam, "true");
}
chrome.tabs.update(tabs[0].id, { url: tabUrl.origin + tabUrl.pathname + '?' + params.toString() });
window.close();
});
},
getPsiData: function() {
chrome.tabs.query({ 'active': true, 'lastFocusedWindow': true }, function(tabs) {
var pageUrl = new URL(tabs[0].url);
var gradeUrl = pageUrl.origin.replace("://", "%3A%2F%2F") + pageUrl.pathname;
trackClick("googlePageSpeed");
$.getJSON('https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=' + gradeUrl + '&fields=id%2CruleGroups', function(data) {
if (data.id) {
$("#desktop_psi_placeholder").html("Desktop PSI Score<span class='c-btn__score'>" + data.ruleGroups.SPEED.score + "</span>");
} else {
console.log('hmmmmm, Googles APIs are really painful, they did not grade for some reason');
}
});
$.getJSON("https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=" + gradeUrl + "&fields=id%2CruleGroups&strategy=mobile", function(data) {
if (data.id) {
$("#mobile_psi_placeholder").html("Mobile PSI Score<span class='c-btn__score'>" + data.ruleGroups.SPEED.score + "</span>");
} else {
console.log("hmmmmm, Googles APIs are really painful, they did not grade for some reason");
}
});
});
},
setMenuContext: function() {
console.log("Set Menu Context run");
chrome.tabs.getSelected(null, function(tab) {
/*getSelected might be deprecated need to review*/
var tabUrl = tab.url;
console.log("Current URL: ", tabUrl);
let appUrl;
if(~tabUrl.indexOf("local.hubspot")) {
appUrl = ~tabUrl.indexOf("local.hubspotqa") ? 'local.hubspotqa.com' : 'local.hubspot.com'
}
else {
appUrl = ~tabUrl.indexOf("app.hubspotqa.com") ? "app.hubspotqa.com" : "app.hubspot.com";
}
console.log(appUrl)
if (~tabUrl.indexOf(appUrl)) {
console.log("This is the hubspot backend.");
$("body").addClass("is-backend"); //indicates user is seeing HS backend
$(".c-tab-slider").addClass('c-tab-slider--state-design-manager');
if (~tabUrl.indexOf("/design-manager/")) {
console.log("Old Design Manager is active");
$("body").addClass("is-dm1"); //indicates user is seeing design manager v1
}
if (~tabUrl.indexOf("/beta-design-manager/")) {
/*note this string detection will likely break once rolled out to everyone as they likely wont leave beta in the name*/
console.log("Design Manager V2 is active");
$("body").addClass("is-dm2"); //indicates user is seeing design manager v2
}
} else if (~tabUrl.indexOf("designers.hubspot.com/docs/")) {
console.log("Viewing HubSpot Documentation");
$("body").addClass("is-hs-docs");
$(".c-tab-slider").addClass("c-tab-slider--state-debug");
} else {
console.log("This is not in the HubSpot Backend");
$(".c-tab-slider").addClass("c-tab-slider--state-debug");
}
});
},
loadTip:function(){
var tips = [
{
tipId:"VSCode Extension",
title:"Use VSCode?",
content:"Did you know about the HubL Language Extension?",
url:"https://marketplace.visualstudio.com/items?itemName=WilliamSpiro.hubl#overview",
},
{
tipId:"ext Slack Channel",
title:"#developer-extension",
content:"We have a channel in the official HS Dev Slack, share feedback there or on github Issues",
url:"https://hubspotdev.slack.com/messages/CBBAW6Z3R",
},
{
tipId:"hubXml",
title:"hubXml",
content:"You can turn any blog into a HubSpot importable XML file with this open source tool",
url:"https://github.com/williamspiro/hubXml",
},
{
tipId:"resize_image_url",
title:'{{ resize_image_url() }}',
content:"You can use HubL to resize images dynamically. Saving marketers from themselves. Do it.",
url:"https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#resize-image-url",
},
{
tipId:"do_tag",
title:'Need to modify lists in HubL? Use the DO tag',
content:"{% do list.append('stuff') %) Just DO it.",
url:"https://designers.hubspot.com/docs/hubl/syntax#expression-statements",
},
{
tipId:"Discovery Kit",
title:'HS Discovery Kit',
content:"New to devloping on HubSpot? get all the best tools, docs and resources in the kit!",
url:"https://designers.hubspot.com/discoverykit",
},
{
tipId:"Hub-Batch",
title:'Hub-Batch',
content:"A library that utilizes HubSpot APIs for bulk updating COS content (Blog Posts and Site Pages)",
url:"https://github.com/williamspiro/hub-batch",
},
{
tipId:"CrankShaft",
title:'CrankShaft',
content:"Help create a framework like Timber or Bootstrap, specifically tailored to HubSpot",
url:"https://github.com/TheWebTech/CrankShaft",
},
{
tipId:"feature breakdown",
title:'Feature Breakdown',
content:"This extension has a lot of features that might be obscure, let us break it down for you, it'll only take 2 minutes.",
url:"https://github.com/williamspiro/HubSpot-Developer-Extension/wiki/Feature-Breakdown",
},
{
tipId:"local dev-cli",
title:'Local development has arrived!',
content:"Using the HubSpot CMS CLI you can work locally using your favorite code editor and tools.",
url:"https://designers.hubspot.com/docs/tools/local-development",
},
{
tipId:"HS Dev Slack",
title:'HubSpot Developer Slack',
content:"The HubSpot Developer Slack is an invaluable resource, keep up with the top HS devs, they hang out there.",
url:"https://designers.hubspot.com/slack",
},
{
tipId:"ext Privacy Policy",
title:'Privacy Policy',
content:"Simply put - we don't collect personally identifiable or confidential info, just basic anonymized usage stats, learn more",
url:"https://github.com/williamspiro/HubSpot-Developer-Extension/wiki/Privacy-Policy",
},
{
tipId:"ext Updates",
title:'Updates',
content:"We post our updates to both the beta and stable build of the extension as a release on our GitHub, check it out to learn about new features.",
url:"https://github.com/williamspiro/HubSpot-Developer-Extension/releases",
},
{
tipId:"ext rate and review",
title:'Like the extension? consider rating/reviewing',
content:"Ratings and reviews increase visibility of the extension, more visibility means more contributors, which means faster rollout of new features.",
url:"https://chrome.google.com/webstore/detail/hubspot-developer-extensi/gebemkdecnlgbcanplbgdpcffpdnfdfo",
},
{
tipId:"ext Kbd shortcuts",
title:'Keyboard Shortcuts',
content:"There are Keyboard Shortcuts for the cache buster and Debug functions",
url:"https://github.com/williamspiro/HubSpot-Developer-Extension/wiki/How-to-Use-and-set-up-Keyboard-Shortcuts",
},
{
tipId:"devtoolschtsheet post",
title:'Dev Tools Cheat Sheet',
content:"In the HS developer forum developers like you have been compiling a cheatsheet of all their best tools that they use for HubSpot development.",
url:"https://community.hubspot.com/t5/Share-Your-Work/Developers-Tools-Cheat-Sheet/m-p/207945/highlight/true#M258",
},
{
tipId:"cmsdevcert",
title:'HubSpot CMS for Developers Certification',
content:"Free HubSpot CMS course for developers. Become a certified HubSpot CMS developer to prove your expertise.",
url:"https://app.hubspot.com/signup/standalone-cms-developer?hubs_signup-url=academy.hubspot.com/courses/cms-for-developers&intent=learningCenter&track=34",
},
];
var randomTip = [Math.floor(Math.random()*tips.length)];
$(".c-banner").attr("data-tipId",tips[randomTip].tipId);
$(".c-banner").attr("href",tips[randomTip].url);
$(".c-banner .tip__title").text(tips[randomTip].title);
$(".c-banner .tip__content").text(tips[randomTip].content);
},
saveSettings: function() {
// Saves settings to chrome.storage
console.log("settings saved");
var darkthemeVal = $("#darktheme").prop("checked");
var uiTweaksVal = $("#uiTweaks").prop("checked");
var sprockyVal = $("#sprocky").prop("checked");
console.log("dark theme is ", darkthemeVal);
console.log("UI Tweaks is ", uiTweaksVal);
console.log("Sprocky is ", sprockyVal);
chrome.storage.sync.set({
darktheme: darkthemeVal,
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.textContent = "Options saved. If you have the Design manager open, you will need to refresh to see the theme.";
setTimeout(function() {
status.textContent = "";
}, 4000);
});
chrome.storage.sync.set({
uitweaks: uiTweaksVal,
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.textContent = "Options saved. If you have the Design manager open, you will need to refresh to see the tweaks.";
setTimeout(function() {
status.textContent = "";
}, 4000);
});
chrome.storage.sync.set({
sprocky2: sprockyVal,
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.textContent = "Option saved. If you have the Design manager open, you will need to refresh to see Sprocky.";
setTimeout(function() {
status.textContent = "";
}, 4000);
});
},
getSettings: function() {
chrome.storage.sync.get(["darktheme"], function(items) {
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
document.getElementById("darktheme").checked = items.darktheme;
console.log("dark theme:", items.darktheme);
if (items.darktheme) {
$(".dark-theme-toggle .uiToggleSwitch").addClass("uiToggleSwitchOn private-form__toggle-switch--on");
}
});
chrome.storage.sync.get(["uitweaks"], function(items) {
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
document.getElementById("uiTweaks").checked = items.uitweaks;
console.log("dark theme:", items.uitweaks);
if (items.uitweaks) {
$(".ui-tweaks-toggle .uiToggleSwitch").addClass("uiToggleSwitchOn private-form__toggle-switch--on");
}
});
chrome.storage.sync.get(["sprocky2"], function(items) {
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
document.getElementById("sprocky").checked = items.sprocky2;
console.log("sprocky:", items.sprocky2);
if (items.sprocky2) {
$(".sprocky-toggle .uiToggleSwitch").addClass("uiToggleSwitchOn private-form__toggle-switch--on");
}
});
},
onLoad: function() {
/* Temporary fix to the height bug in the popup. This should get removed soon. */
window.setTimeout(() => {
$("html, body").css({
height: $(".c-tab-slider").outerHeight()
});
}, 100);
/* end temporary bug fix */
if (! (window.location.href.indexOf("content/") && window.location.href.indexOf("edit/"))){
document.getElementById("developerMode").style.display = "none";
}
developerTools.setMenuContext();
developerTools.getSettings();
developerTools.loadTip();
/*document.addEventListener('DOMContentLoaded', developerTools.getSettings());
document.getElementById('save').addEventListener('click',
developerTools.saveSettings());*/
$(".js-click--debug,.js-click--move-jquery-to-footer,.js-click--bust-cache,.js-click--amp,.js-click--developerMode").click(function() {
developerTools.debugReload($(this).attr("id"));
});
$(".js-click--psi-score-request").click(function() {
$(".c-btn__psiScore").css("display", "block"); /*move to css?*/
developerTools.getPsiData();
$(".js-click--psi-score-request").addClass("c-btn--graded");
});
$(".js-click--tab-debug").click(function() {
console.log("debug");
$(".c-tab-slider").removeClass("c-tab-slider--state-design-manager");
$(".c-tab-slider").addClass("c-tab-slider--state-debug");
});
$(".js-click--tab-develop").click(function() {
console.log("develop");
$(".c-tab-slider").removeClass("c-tab-slider--state-debug");
$(".c-tab-slider").addClass("c-tab-slider--state-design-manager");
});
/*these settings could be combined into one function.*/
$(".dark-theme-toggle input").change(function() {
developerTools.saveSettings();
$(".dark-theme-toggle .uiToggleSwitch").toggleClass("uiToggleSwitchOn private-form__toggle-switch--on");
});
$(".ui-tweaks-toggle input").change(function() {
developerTools.saveSettings();
$(".ui-tweaks-toggle .uiToggleSwitch").toggleClass("uiToggleSwitchOn private-form__toggle-switch--on");
});
$(".sprocky-toggle input").change(function() {
developerTools.saveSettings();
$(".sprocky-toggle .uiToggleSwitch").toggleClass("uiToggleSwitchOn private-form__toggle-switch--on");
});
$("a.c-banner").click(function(e){
var tipId = "tip:" + $(this).data("tipid");
trackClick(tipId);
})
}
};
window.onload = developerTools.onLoad();