-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMESimplePermalink.js
161 lines (141 loc) · 8.05 KB
/
WMESimplePermalink.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
// ==UserScript==
// @name WME Simple Permalink (from WME KeepMyLayers)
// @namespace https://greasyfork.org/users/11629-TheLastTaterTot
// @version 0.1.6
// @description Shortens WME permalinks by removing any layer and filter specifications
// @author TheLastTaterTot
// @include https://beta.waze.com/*editor*
// @include https://www.waze.com/*editor*
// @exclude https://www.waze.com/*user/editor/*
// @grant none
// @run-at document-end
// ==/UserScript==
/* jshint -W097 */
var initSimplePermalink = function() {
if (!document.getElementById('kmlPLPlaceholder')) {
var kmlKeyPresses = Array(2);
var getKMLPermalink = function(currPl) {
var kmlShortPL = currPl.substr(currPl.lastIndexOf('editor')).replace(/&[^&]*Filter=[^&]*|&s=(\d+)/ig,'');
return location.origin + '/' + kmlShortPL;
};
var copyToClipboard = function(str) {
var $temp = $('<input>');
$('body').append($temp);
$temp.val(str).select();
document.execCommand('copy');
$temp.remove();
};
var copyPLHotkeyEvent = function(e) {
if (e.metaKey || e.ctrlKey) kmlKeyPresses[0] = true;
if (e.which === 67) kmlKeyPresses[1] = true;
if (kmlKeyPresses[0] && kmlKeyPresses[1]) {
copyToClipboard(document.getElementById('aKMLPermalink').getAttribute('href'));
document.getElementById('kmlPLTooltip').style.display = 'none';
document.getElementById('kmlPLTooltipCopied').style.display = 'block';
setTimeout(function() {
document.getElementById('kmlPLTooltipCopied').style.display = 'none';
}, 2000);
}
};
var kmlStyle = document.createElement("style");
// Create CSS container element
kmlStyle.type = "text/css";
kmlStyle.id = "kml-css-container";
kmlStyle.innerHTML = `
.kml-pl-container { height: 25px; width: 48px; position: absolute; bottom: 0; right: 0; line-height: 24px; margin-right: 15px; margin-left: -24px; padding-left: 2px; visibility: visible; pointer-events: auto; }
.kml-pl-container a:link, .kml-pl-container a:active, .kml-pl-container a:focus { text-decoration: none; background-image: none; outline: 0; -webkit-box-shadow: none; box-shadow: none; }
.kml-pl-container>.fa-stack { height: 25px; width: 24px; margin-left: -2px; line-height: inherit; }
.kml-pl-container>.fa-stack .fa-circle { font-size: 26px; line-height: 25px; bottom: 0px; }
.kml-pl-container>.fa-stack .fa-link { font-size: 16px; line-height: 25px; bottom: 0px; }
.kml-pl-tooltipbox { max-width: 99%; right: 0; white-space: normal; word-wrap: break-word; word-break: break-all; bottom: 25px; visibility: visible; margin-right: 15px; color: white; font-size: 8pt; background-color: transparent; pointer-events: none; position: absolute; }
.kml-pl-tooltipbox>div { padding: 5px; border-radius: 5px; background-color: black; }
.street-view-mode .kml-pl-container, .street-view-mode .kml-pl-tooltipbox { right: 50% !important; }`;
document.head.appendChild(kmlStyle);
var wazePermalinkEl = document.querySelector('.WazeControlPermalink>a.fa-link'),
wazeCopyPlNote = wazePermalinkEl.getAttribute('data-original-title'),
kmlCurrentPl = getKMLPermalink(wazePermalinkEl.getAttribute('href')),
wazeControlPermalinkEl = wazePermalinkEl.parentNode,
kmlMapPLContainer = document.createElement('div'),
kmlPLPlaceholder = document.createElement('div'),
kmlPermalink;
wazePermalinkEl.id = 'wazePermalink';
kmlMapPLContainer.id = 'kmlPL';
kmlMapPLContainer.style.position = 'absolute';
kmlMapPLContainer.style.width = '100%';
kmlMapPLContainer.style.bottom = '0px';
kmlMapPLContainer.style.right = '0px';
kmlMapPLContainer.style.visibility = 'hidden';
kmlMapPLContainer.style.pointerEvents = 'none';
kmlMapPLContainer.style.zIndex = 4;
kmlMapPLContainer.innerHTML = '<div class="kml-pl-tooltipbox"><div id="kmlPLTooltip" style="display: none;"></div><div id="kmlPLTooltipCopied" style="display: none;"></div></div>' +
'<div class="kml-pl-container" style="overflow: hidden; width: 25px;"><div id="kmlPermalink" class="fa-stack"></div></div>';
document.getElementById('map').appendChild(kmlMapPLContainer);
kmlPLPlaceholder.id = 'kmlPLPlaceholder';
kmlPLPlaceholder.style.float = 'right';
kmlPLPlaceholder.style.position = 'relative';
kmlPLPlaceholder.style.bottom = '0px';
kmlPLPlaceholder.style.right = '0px';
kmlPLPlaceholder.style.height = '25px';
kmlPLPlaceholder.style.width = '25px';
kmlPLPlaceholder.style.marginRight = '-4px';
kmlPLPlaceholder.style.marginLeft = '-24px';
kmlPLPlaceholder.style.backgroundColor = '#E9E9E9';
wazeControlPermalinkEl.appendChild(kmlPLPlaceholder);
//-------------------------
kmlPermalink = document.getElementById('kmlPermalink');
kmlPermalink.innerHTML = '<a id="aKMLPermalink" href="' + kmlCurrentPl + '"><span class="fa fa-circle fa-stack-1x" style="color: #59899E;"></span><span class="fa fa-link fa-stack-1x fa-inverse"></span></a>';
//-------------------------
// PL address popup
document.getElementById('kmlPLTooltip').innerHTML = '<span id="tooltipKMLPermalink">' + kmlCurrentPl + '</span><p></p><b>' + wazeCopyPlNote + '</b>';
// "Copied" popup
document.getElementById('kmlPLTooltipCopied').innerHTML = '<b>' + I18n.translations[I18n.locale].footer.link_copied + '</b>';
//------------------------------------------------------------------
kmlPermalink.addEventListener('mouseenter', function(e) {
var changedThisPl = getKMLPermalink(wazePermalinkEl.getAttribute('href'));
document.getElementById('tooltipKMLPermalink').innerHTML = changedThisPl;
document.getElementById('aKMLPermalink').setAttribute('href', changedThisPl);
document.getElementById('kmlPLTooltip').style.display = 'block';
window.addEventListener('keydown', copyPLHotkeyEvent, false);
}, false);
kmlPermalink.addEventListener('mouseleave', function() {
kmlKeyPresses = Array(2);
document.getElementById('kmlPLTooltip').style.display = 'none';
document.getElementById('kmlPLTooltipCopied').style.display = 'none';
window.removeEventListener('keydown', copyPLHotkeyEvent);
}, false);
try {
// Hide WME permalink, but allow TB to overrule with display: none;
wazePermalinkEl.style.visibility = 'hidden';
} catch (err) {}
}
};
////////////////////////////////////////////////////////////////////////////////////
function waitForWazeElement() {
var waitCount = 0,
maxWait = 50; //30++ seconds
var tryInit_kmlPL = function() {
try {
if (waitCount < maxWait &&
document.getElementsByClassName('WazeControlPermalink').length) {
initSimplePermalink();
waitCount++; //for catching returns due to an undetected error
} else if (waitCount++ < maxWait) {
setTimeout(tryInit_kmlPL, 25 * waitCount);
} else {
console.error('WMESPL:',
'Could not find necessary WME permalink element');
}
} catch (err) {
try { //try again once more in case it failed due to another script or WME hiccup... >:]
setTimeout(tryInit_kmlPL, 500);
} catch (err) {
console.error(
'WMESPL:',
'WME Simple Permalinks failed to load due to some kind of technical script error. :(');
console.error(err);
}
}
};
tryInit_kmlPL();
}
setTimeout(waitForWazeElement,2000);