Skip to content

Instantly share code, notes, and snippets.

@cmdcolin
Created November 28, 2023 14:35
Show Gist options
  • Save cmdcolin/b71f0567597127bf869804a660b3bc46 to your computer and use it in GitHub Desktop.
Save cmdcolin/b71f0567597127bf869804a660b3bc46 to your computer and use it in GitHub Desktop.
JBrowse 2 plugin to add navigation to a specific location
/* eslint-disable no-restricted-globals */
;(function () {
class MyLocPlugin {
name = 'MyLocPlugin'
version = '1.0'
install(pluginManager) {
/* do nothing */
}
configure(pluginManager) {
const session = pluginManager.rootModel.session
const view = session.views[0]
const url = new URL(window.location)
const myloc = url.searchParams.get('myloc') // use a different term than &loc=
if (myloc) {
// asynchronously navigates to the location specified by the url param
// e.g. &myloc=chr1:1-100
view.navToLocString(myloc).catch(e => {
// asynchronously catch the error and alert if something went wrong
session.notify(`${e}`, 'error')
})
// optional: remove the URL parameter
url.searchParams.delete('myloc')
window.history.replaceState(null, null, url)
}
}
}
// the plugin will be included in both the main thread and web worker, so
// install plugin to either window or self (webworker global scope)
;(typeof self !== 'undefined' ? self : window).JBrowsePluginMyLocPlugin = {
default: MyLocPlugin,
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment