Skip to content

Instantly share code, notes, and snippets.

@GHolk
Last active February 4, 2018 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GHolk/6b3840593907fd503931ea304edb4734 to your computer and use it in GitHub Desktop.
Save GHolk/6b3840593907fd503931ea304edb4734 to your computer and use it in GitHub Desktop.
greasy monkey script, when click hyperlink with win key down, will open link in new window without toolbar, location bar. if in youtube, would open embed player page.
// ==UserScript==
// @name open clean window
// @namespace http://gholk.github.io/
// @description when click mouse with WinKey, open ancher in new window without toolbar, manubar, location bar.
// @match <all_urls>
// @version 10
// @grant none
// ==/UserScript==
/* greasy monkey script, when click hyperlink with win key down,
* will open link in new window without toolbar, location bar.
*
* if link is youtube, would open embed player page.
* if click on blank regiion in `youtube.com/watch?v=*` ,
* would open new blank page too.
*/
const captureEvent = true
window.addEventListener('click', clickHandler, captureEvent)
const comfortable = {}
comfortable.list = []
comfortable.convert = function (url) {
const result = this.list
.map(converter => converter(url))
.filter(result => result)
if (result.length == 0) return url
else return result.pop()
}
comfortable.list.push(convertYoutube)
function convertYoutube (url) {
const parseUrl = new URL(url)
if (isYoutube(parseUrl)) return convertEmbed(parseUrl)
else return false
function isYoutube(url) {
const host = url.host
return /youtube\.com$/.test(host) ||
/youtu\.be$/.test(host)
}
function convertEmbed(url) {
const scan = url.search.match(/[&\/\?]v=([^&]*)/)
const id = scan[1]
return 'https://youtube.com/embed/' + id
}
}
function openCleanWindow(url, title, option) {
if (!title) title = 'clean window'
if (!option) option = 'resizable'
return window.open(url, title, option)
}
function findAnchor(node) {
if (node.tagName == 'A') return node
else {
const parent = node.parent
if (parent) return findAnchor(parent)
else return false
}
}
function isModifierClick(click) {
return click.getModifierState('OS')
}
// stop event propagation, prevent other framework action.
function stopEvent(event) {
event.stopImmediatePropagation()
event.preventDefault()
}
function clickHandler(click) {
if (isModifierClick(click)) {
const anchor = findAnchor(click.target)
if (anchor) {
stopEvent(click)
const convertUrl = comfortable.convert(anchor.href)
openCleanWindow(convertUrl)
}
else {
const convertUrl = comfortable.convert(location.href)
if (convertUrl) {
openCleanWindow(convertUrl)
window.close()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment