Last active
December 16, 2021 11:58
-
-
Save johnd0e/a06ea19aefa6cbf35651eca9bee470a3 to your computer and use it in GitHub Desktop.
IITC plugin: Browsed portals history [https://gist.github.com/johnd0e/a06ea19aefa6cbf35651eca9bee470a3/raw/browsed-portals-history.user.js]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @author jd | |
// @name IITC plugin: Browsed portals history | |
// @category Misc | |
// @version 0.1 | |
// @description Pushes browsed portals into browser's history, and allows navigate between them with back/forward buttons. | |
// @namespace https://gist.github.com/johnd0e | |
// @homepageURL https://gist.github.com/johnd0e/a06ea19aefa6cbf35651eca9bee470a3 | |
// @supportURL https://gist.github.com/johnd0e/a06ea19aefa6cbf35651eca9bee470a3#new_comment_field | |
// @downloadURL https://gist.github.com/johnd0e/a06ea19aefa6cbf35651eca9bee470a3/raw/custom-linked-portals-show.user.js | |
// @id browsed-portals-history | |
// @match https://intel.ingress.com/* | |
// @grant none | |
// ==/UserScript== | |
function wrapper(plugin_info) { | |
// ensure plugin framework is there, even if iitc is not yet loaded | |
if(typeof window.plugin !== 'function') window.plugin = function() {}; | |
var skipHook; | |
window.addEventListener('popstate', function (event) { | |
skipHook = true; | |
var ll = getURLParam('pll'); | |
if (ll) { | |
var position = L.latLng(ll.split(',')); | |
selectPortalByLatLng(position); | |
if (!map.getBounds().contains(position)) { | |
map.panInside(position); | |
} | |
} else { | |
renderPortalDetails(null); | |
} | |
skipHook = false; | |
}); | |
function setup () { | |
addHook('portalSelected', function (data) { | |
if (skipHook || data.selectedPortalGuid === data.unselectedPortalGuid) { return; } | |
if (data.selectedPortalGuid) { | |
var portal = portals[data.selectedPortalGuid]; | |
history.pushState(data.selectedPortalGuid, '', makePermalink(portal.getLatLng())); | |
} else { | |
history.pushState(null, '', '/'); | |
} | |
}); | |
if (typeof android === 'undefined') { | |
return; | |
} | |
$('<style>').prop('type', 'text/css').html('.iitc-history-nav { font-size: 20px }').appendTo('head'); | |
var HistoryNav = L.Control.extend({ | |
options: { | |
position: 'topleft' | |
}, | |
onAdd: function (map) { | |
var button = document.createElement('a'); | |
button.text = '⬅'; | |
button.className = 'leaflet-bar-part iitc-history-nav'; | |
button.addEventListener('click', function (ev) { | |
if (selectedPortal) { | |
var position = portals[selectedPortal].getLatLng(); | |
if (!map.getBounds().contains(position)) { | |
map.panInside(position, { padding: [15, 15]}); | |
return; | |
} | |
} | |
history.back(); | |
}, false); | |
button.addEventListener('contextmenu', function (ev) { | |
ev.preventDefault(); | |
history.forward(history); | |
}, false); | |
button.title = 'Long tap [contextmenu] for Forward'; | |
var container = document.createElement('div'); | |
container.className = 'leaflet-bar'; | |
container.appendChild(button); | |
return container; | |
}, | |
}); | |
var ctrl = new HistoryNav; | |
ctrl.addTo(window.map); | |
} | |
var initial = Math.random(); | |
history.replaceState(initial, ''); | |
history.pushState(null, ''); | |
window.addEventListener('popstate', function (event) { | |
if (event.state === initial) { | |
if (typeof android === 'undefined' && confirm('Do you really want to leave?')) { | |
history.back(); | |
} else { | |
history.forward(); | |
} | |
} | |
}); | |
setup.info = plugin_info; //add the script info data to the function as a property | |
if(!window.bootPlugins) window.bootPlugins = []; | |
window.bootPlugins.push(setup); | |
// if IITC has already booted, immediately run the 'setup' function | |
if(window.iitcLoaded && typeof setup === 'function') setup(); | |
} // wrapper end | |
// inject code into site context | |
var script = document.createElement('script'); | |
var info = {}; | |
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description }; | |
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');')); | |
(document.body || document.head || document.documentElement).appendChild(script); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment