-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbackground.js
More file actions
173 lines (162 loc) · 5.68 KB
/
Copy pathbackground.js
File metadata and controls
173 lines (162 loc) · 5.68 KB
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
/* global chrome */
/*
* When the document has a mapml element, set the page content type to text/html,
* reload page, execute scripts
*/
chrome.runtime.onMessage.addListener(function (message, sender) {
if (!sender.tab && sender.url === chrome.runtime.getURL('popup.html') && message.type === 'options') {
chrome.storage.local.set({options: message.options});
if (message.options) {
registerContentScripts();
} else {
unregisterContentScripts();
}
return;
}
if (!sender.tab) {
// This message did not come from a tab and has an unexpected format.
console.warn('BG got unexpected message', message, sender);
return;
}
const tabId = sender.tab.id;
if (message === "html") {
executeScripts(tabId);
try {
// If document initially had "text/html" content type, we do not have a rule registered.
chrome.declarativeNetRequest.updateSessionRules({
removeRuleIds: [tabId]
});
} catch {};
} else if (message === "xml") {
chrome.declarativeNetRequest.updateSessionRules({
addRules: [{
"id": tabId,
"priority": 1,
"action": {
"type" : "modifyHeaders",
"responseHeaders": [{"header": "Content-Type", "operation": "set", "value": "text/html"}]
},
"condition": {"resourceTypes": ["main_frame"], "tabIds": [tabId]}
}]
}, () => chrome.tabs.reload(tabId));
} else if (message === "mapml") {
executeScripts(tabId);
}
});
function registerContentScripts() {
chrome.scripting.registerContentScripts([
{
runAt: "document_start",
id: "content",
matches: [ "<all_urls>" ],
js: [ "content.js" ]
},
{
runAt: "document_idle",
id: "sniffer",
matches: [ "<all_urls>" ],
js: [ "sniffForMapML.js" ]
}
]);
}
async function unregisterContentScripts() {
return chrome.scripting.unregisterContentScripts();
}
/**
* Runs on installs and updates once
*/
chrome.runtime.onInstalled.addListener(async () => {
await unregisterContentScripts();
chrome.storage.local.get("options", function (obj) {
if (obj.options) {
if (obj.options) {
registerContentScripts();
}
} else {
chrome.storage.local.set({
options: {
announceMovement: true,
announceScale: 'metric',
featureIndexOverlayOption: false,
renderMap: true,
defaultExtCoor: 'pcrs',
defaultLocCoor: 'gcrs'
}
});
registerContentScripts();
}
});
});
function createMap() {
let el = document.querySelector("mapml-") ? document.querySelector("mapml-") : document.querySelector("pre");
let mapml = el;
if(el.nodeName === "PRE") {
let parser = new DOMParser();
let doc = parser.parseFromString(el.innerText, "application/xml");
if(doc.querySelector("mapml-")) mapml = doc;
else return false;
}
document.body.removeChild(el);
let map = document.createElement("mapml-viewer");
let defaultProjection = "OSMTILE",
projection = defaultProjection, p,
knownProjections = ["OSMTILE","CBMTILE","APSTILE","WGS84"];
if(mapml.querySelector("map-extent[units]"))
p = mapml.querySelector("map-extent[units]").getAttribute("units").toUpperCase();
if (knownProjections.includes(p)) projection = p;
else if(mapml.querySelector("map-meta[name=projection]"))
p = mapml.querySelector("map-meta[name=projection]").getAttribute("content").toUpperCase();
if (knownProjections.includes(p)) projection = p;
//content="text/mapml;projection=..."
else if(mapml.querySelector("map-meta[content*=projection]")) {
let content = mapml.querySelector("map-meta[content*=projection]").getAttribute("content");
p = content.match("projection=(\\w*)")[1].toUpperCase();
if (knownProjections.includes(p)) projection = p;
}
//Matches #int,float,float at the end of url
let hash = window.location.href.match("([#])(\\d)(,[-]?\\d+[.]?\\d+)(,[-]?\\d+[.]?\\d+)$");
let lat = hash ? hash[4].slice(1) : "0";
let lon = hash ? hash[3].slice(1) : "0";
let zoom = hash ? hash[2] : "0";
let focus = !hash;
map.setAttribute("projection", projection);
map.setAttribute("controls", "");
map.setAttribute("controlslist", "search");
map.setAttribute("lat", lat);
map.setAttribute("lon", lon);
map.setAttribute("zoom", zoom);
let layer = document.createElement("map-layer");
layer.setAttribute("src", window.location.href);
layer.setAttribute("checked", "");
let title = document.createElement("title");
title.innerText = mapml.querySelector('map-title').innerText;
document.head.insertAdjacentElement('afterbegin',title);
layer.addEventListener("loadedmetadata", function () {
if(focus) layer.zoomTo();
});
map.appendChild(layer);
map.addEventListener("map-moveend", function () {
let map = document.querySelector("mapml-viewer");
//Focus fires moveend so if the url has no initial hash, return
if(focus) {
focus = false;
return;
}
window.history.replaceState('','','#' + map.zoom + "," + map.lon + "," + map.lat);
});
document.body.appendChild(map);
window.addEventListener("hashchange", function (e) {
let hash = e.newURL.match("([#])(\\d)(,[-]?\\d+[.]?\\d+)(,[-]?\\d+[.]?\\d+)$");
map.zoomTo(hash[4].slice(1), hash[3].slice(1), hash[2]);
});
return true;
}
function executeScripts(tabId) {
chrome.scripting.executeScript({target: {tabId: tabId}, func: createMap},
(result) => {
if(!result[0].result) return;
chrome.scripting.insertCSS({target: {tabId: tabId}, files: ['resources/map.css']});
chrome.scripting.executeScript({target: {tabId: tabId}, files: ['resources/webcomponents-bundle.js',
'resources/importMapml.js']});
});
}