Skip to content

Commit 34bbf66

Browse files
committed
More robust hooking into Activity/Segment map
Poll/wait until the needed components are in place.
1 parent ee83106 commit 34bbf66

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

common.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Map switcher for Strava website.
3+
*
4+
* Copyright © 2016 Tomáš Janoušek.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
*
8+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
const MapSwitcher = {
14+
sleep(ms) {
15+
return new Promise(resolve => setTimeout(resolve, ms));
16+
},
17+
18+
async wait(what) {
19+
for (let tries = 0, delay = 100; tries < 60; ++tries, delay = Math.min(delay * 2, 1000)) {
20+
const got = what();
21+
if (got)
22+
return got;
23+
24+
await this.sleep(delay);
25+
}
26+
27+
throw new Error(`timeout ${what}`);
28+
},
29+
};

fix-mapbox.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,13 @@ document.arrive(".mapboxgl-map", {onceOnly: false, existing: true, fireOnAttribu
113113
return found ? found[1] : null;
114114
}
115115

116-
function sleep(ms) {
117-
return new Promise(resolve => setTimeout(resolve, ms));
118-
}
119-
120-
async function wait(what) {
121-
for (let tries = 0, delay = 100; tries < 60; ++tries, delay = Math.min(delay * 2, 1000)) {
122-
const got = what();
123-
if (got)
124-
return got;
125-
126-
await sleep(delay);
127-
}
128-
129-
throw new Error(`timeout ${what}`);
130-
}
131-
132116
async function patchReactMapbox(mapbox) {
133-
const map = await wait(function () {
117+
const map = await MapSwitcher.wait(function () {
134118
let map = null;
135119
mapbox.return.memoizedProps.mapboxRef((m) => (map = m, m));
136120
return map;
137121
});
138-
await wait(() => map.getLayer("global-heatmap") || map.getLayer("personal-heatmap"));
122+
await MapSwitcher.wait(() => map.getLayer("global-heatmap") || map.getLayer("personal-heatmap"));
139123

140124
function setMapType(t) {
141125
if (t && !AdditionalMapLayers[t])

fix.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
*/
1212

1313
document.arrive(".leaflet-container", {onceOnly: false, existing: true, fireOnAttributesModification: true}, function () {
14-
if (this.mapSwitcherDone) return;
15-
this.mapSwitcherDone = true;
14+
const leafletContainer = this;
15+
16+
if (leafletContainer.mapSwitcherDone) return;
17+
leafletContainer.mapSwitcherDone = true;
1618

1719
function tileLayer(l) {
1820
var r = L.tileLayer(l.url, l.opts);
@@ -50,8 +52,10 @@ document.arrive(".leaflet-container", {onceOnly: false, existing: true, fireOnAt
5052
};
5153
Object.entries(AdditionalMapLayers).forEach(([type, l]) => layerNames[type] = l.name);
5254

53-
var activityOpts = jQuery('#map-type-control .options', this);
54-
if (activityOpts.length) {
55+
MapSwitcher.wait(function () {
56+
const q = jQuery('#map-type-control .options', leafletContainer);
57+
return q.length ? q : null;
58+
}).then(function (activityOpts) {
5559
Strava.Maps.CustomControlView.prototype.handleMapTypeSelector = function (t) {
5660
const type = this.$$(t.target).data("map-type-id");
5761
const selected = this.$("#selected-map");
@@ -108,10 +112,12 @@ document.arrive(".leaflet-container", {onceOnly: false, existing: true, fireOnAt
108112
activityOpts.removeClass("open-menu");
109113
activityOpts.parent().removeClass("active");
110114
}
111-
}
115+
});
112116

113-
var explorerMapFilters = jQuery('#segment-map-filters form');
114-
if (explorerMapFilters.length) {
117+
MapSwitcher.wait(function () {
118+
const q = jQuery('#segment-map-filters form');
119+
return q.length ? q : null;
120+
}).then(function (explorerMapFilters) {
115121
var once = false;
116122
function explorerFound(e) {
117123
if (once)
@@ -160,5 +166,5 @@ document.arrive(".leaflet-container", {onceOnly: false, existing: true, fireOnAt
160166
Strava.Explorer.Navigation.prototype.navigate = old_navigate;
161167
};
162168
explorerMapFilters.trigger('submit');
163-
}
169+
});
164170
});

load.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
loadJQuery().then(() => Promise.all([
4747
getScript(getURL('arrive.min.js')),
48+
getScript(getURL('common.js')),
4849
getScript(getURL('layers.js')),
4950
getScript(getURL('donation.js')),
5051
ignoreError(loadGoogleMaps().then(() => Promise.all([

manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"3rd/leaflet-pegman.min.css",
2727
"3rd/leaflet-pegman.min.js",
2828
"arrive.min.js",
29+
"common.js",
2930
"donation.js",
3031
"fix-mapbox.js",
3132
"fix.js",

0 commit comments

Comments
 (0)