-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
archive.js
102 lines (94 loc) · 3.11 KB
/
archive.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
// archive.js
// do NOT use jQuery in this file
let gCloseClicked = false
let gArchiveClicked = false
let gShadowRoot
const ERROR_CODE_DIC = {
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Auth Required',
408: 'Request Timeout',
410: 'Page Gone',
429: 'Too Many Requests',
451: 'Unavailable',
500: 'Internal Server Error',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
509: 'Bandwidth Limit Exceeded',
520: 'Unknown Error',
521: 'Server Is Down',
522: 'Connection Timed Out',
523: 'Unreachable Origin',
524: 'Timout Occurred',
525: 'SSL Handshake Failed',
526: 'Invalid SSL Certificate',
999: 'Server Not Found'
}
// appending css to the popup
function appendStyle() {
const url = chrome.runtime.getURL('css/archive.css')
return `<link rel="stylesheet" type="text/css" href=${url}>`
}
// appending the actual dom of popup
function appendHTML(url, code) {
const title = ((code < 999) ? code + ' ' : '') + (ERROR_CODE_DIC[code] || 'Error')
const close = chrome.runtime.getURL('images/close.svg')
const logo = chrome.runtime.getURL('images/wayback-light.png')
const caption = 'View a saved version courtesy of the'
const archive = 'View Archived Version'
return `
<div id="popup-container">
<div id="title-txt">${title}</div>
<img id="close-btn" src=${close}>
<p>${caption}</p>
<img class="wm-logo" alt="Internet Archive Wayback Machine" src=${logo}>
<a href=${url} id="archive-btn">${archive}</a>
</div>`
}
function popupWayback(url, code) {
// Using shadow DOM to encapsulate the popup
const container = document.createElement('div')
container.id = 'waybackmachine-container'
gShadowRoot = container.attachShadow({ mode: 'open' })
gShadowRoot.innerHTML = appendStyle() + appendHTML(url, code)
document.body.insertAdjacentElement('beforeend', container)
// Adding functionality to close and archive button
const closeBtn = gShadowRoot.querySelector('#close-btn')
closeBtn.addEventListener('click', () => {
gCloseClicked = true
let popup = gShadowRoot.querySelector('#popup-container')
popup.style.display = 'none'
})
const archiveBtn = gShadowRoot.querySelector('#archive-btn')
archiveBtn.addEventListener('click', (e) => {
gArchiveClicked = true
// Work-around for myspace which hijacks the link
if (window.location.hostname.indexOf('myspace.com') >= 0) {
e.preventDefault()
return false
}
})
}
// Polling for DOM update
function refreshWayback(url, code) {
const container = document.querySelector('#waybackmachine-container')
if (!gCloseClicked && !gArchiveClicked && !container) {
popupWayback(url, code)
setTimeout(() => { refreshWayback(url, code) }, 500)
}
}
// Listens to SHOW_BANNER messages
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
if (request.type === 'SHOW_BANNER') {
if (('status_code' in request) && ('wayback_url' in request)) {
refreshWayback(request.wayback_url, request.status_code)
}
}
}
)