Last active
May 24, 2024 17:12
-
-
Save alekstar79/720e512f37d7482fe2e7b01f078a93eb to your computer and use it in GitHub Desktop.
Working with blob-events-fs
This file contains hidden or 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
const nodes = [] | |
export function removeNodes() | |
{ | |
nodes.forEach(el => el.parentNode && el.parentNode.removeChild(el)) | |
nodes.splice(0, nodes.length) | |
} | |
/** | |
* @param {String} link | |
* @param {Function} fallback | |
* @return {HTMLIFrameElement} | |
*/ | |
export function redirect(link, fallback) | |
{ | |
removeNodes() | |
const html = `<body><script>window.top.location.replace('${link}')</script></body>`, | |
blob = new Blob([html], { type: 'text/html' }), | |
iframe = document.createElement('iframe') | |
iframe.style.display = 'none' | |
iframe.setAttribute('sandbox', 'allow-popups allow-scripts allow-top-navigation') | |
iframe.setAttribute('id', 'top_level_redirect') | |
iframe.addEventListener('load', function() { | |
setTimeout(() => { | |
this.remove() | |
fallback() | |
}, 1400) | |
}) | |
iframe.src = window.URL.createObjectURL(blob) | |
document.body.appendChild(iframe) | |
nodes.push(iframe) | |
return iframe | |
} | |
/** | |
* @param {String} link | |
* @param {String} target | |
*/ | |
export function openLink(link, target = '_blank') | |
{ | |
removeNodes() | |
const a = document.createElement('a'), | |
click = new MouseEvent('click', { | |
cancelable: true, | |
bubbles: true, | |
view: window | |
}) | |
a.setAttribute('target', target || '_blank') | |
a.setAttribute('href', link) | |
document.body.appendChild(a) | |
nodes.push(a) | |
a.dispatchEvent(click) | |
} | |
/** | |
* @param {String} type | |
* @param {Boolean} multiple | |
* @param {Boolean} capture | |
* @return {Promise<(String|Blob)>} | |
*/ | |
export function openFile(type, multiple = false, capture = false) | |
{ | |
removeNodes() | |
return new Promise(resolve => { | |
const input = document.createElement('input'), | |
click = new MouseEvent('click', { | |
cancelable: true, | |
bubbles: true, | |
view: window | |
}) | |
input.multiple = multiple | |
input.accept = type | |
input.type = 'file' | |
if (capture) { | |
input.capture = 'camera' | |
} | |
hide(input) | |
input.addEventListener('change', () => { | |
const files = Array.from(input.files) | |
multiple ? resolve(files) : resolve(files[0]) | |
}, false) | |
document.body.appendChild(input) | |
nodes.push(input) | |
input.dispatchEvent(click) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment