-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
106 lines (87 loc) · 2.78 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
function isSupported() {
try {
return isJiraTab() || isZendeskTab() || isDeviceFarmTab() || isTrelloTab() || isIntercomTab() || isSauceLabsTab() || isPerfectoTab();
} catch (error) {
console.error("Error during tab detection:");
console.error(error);
return false;
}
}
function loadExtension() {
if ((document.readyState === "interactive" || document.readyState === "complete")) {
addTimer();
// Listen messages from other windows (i.e TestFairy iframes to get notified about content dimensions)
window.addEventListener("message", receiveMessage, false);
}
}
function receiveMessage(event) {
var origin = event.origin || event.originalEvent.origin; // For Chrome, the origin property is in the event.originalEvent object.
if (event.data.height) {
var height = (event.data.height) + "px";
var elementById = document.getElementById(getTestFairyCommonIFrameId());
if (elementById) {
elementById.height = height;
}
var elementByClass = document.querySelectorAll("." + getTestFairyCommonIFrameId());
if (elementByClass.length > 0) {
elementByClass.forEach(element => {
element.height = height;
});
}
}
}
var remainingAttempts = 64;
function addTimer() {
// SPA friendly reattempts
if (remainingAttempts > 0) {
setTimeout(addTimer, 2000);
}
// Try again
if (!isSupported()) {
remainingAttempts--;
return;
}
// If very last attempt was lucky, reenable timer
if (remainingAttempts == 0) {
setTimeout(addTimer, 2000);
}
// Reset remainingAttempts on successful attempt
remainingAttempts = 64;
var testFairyFrame = document.querySelector(getTestFairyCommonIFrameSelector());
if ((isJiraTab() || isZendeskTab() || isTrelloTab() || isSauceLabsTab() || isPerfectoTab()) && testFairyFrame) {
// No need to load extension twice for Jira, Zendesk, Trello and Sauce Labs if iframe already exists
// Device farm uses multiple iframes without any specific ids, thus must be processed always
cleanPerfectoIfNecessary();
return;
}
if (isJiraTab()) {
addTestFairyJiraIFrame();
}
if (isZendeskTab()) {
addTestFairyZendeskIFrame();
}
if (isDeviceFarmTab()) {
addTestFairyDeviceFarmIFrame();
}
if (isTrelloTab()) {
addTestFairyTrelloIFrame();
}
if (isIntercomTab()) {
addIntercomIFrame();
}
if (isSauceLabsTab()) {
addSauceLabsIFrame();
proceedToDeviceSelection();
}
if (isPerfectoTab()) {
addPerfectoIFrame();
}
}
////////////////////////////////// Startup
if (chrome && chrome.extension && chrome.extension.sendMessage) {
chrome.extension.sendMessage({}, loadExtension); // Chrome
} else if (browser && browser.runtime && browser.runtime.sendMessage) {
browser.runtime.sendMessage({}).then(loadExtension).catch(loadExtension); // Firefox (WebExtensions)
} else {
throw new Error('TestFairy extension does not support this browser!');
}