Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Created April 20, 2025 13:26
Show Gist options
  • Save andersonbosa/2083537f18f2ada12a96d423d6f66f62 to your computer and use it in GitHub Desktop.
Save andersonbosa/2083537f18f2ada12a96d423d6f66f62 to your computer and use it in GitHub Desktop.
TamperMonkey Script to intercept all urls
// ==UserScript==
// @name Flexible URL Interceptor
// @description Intercepta URLs experadas e executa ação
// @author https://github.com/andersonbosa
// @version 1.0.0
// @namespace http://tampermonkey.net/
// @grant none
// @match https://*/*
// ==/UserScript==
(function () {
'use strict'
const TARGETS = [
'https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F',
'https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com'
]
function onMatch(src) {
console.log('[MATCH]', src)
const vimeoUrl = new URL(src).searchParams.get('src')
alert(vimeoUrl)
}
function matchTarget(src) {
return TARGETS.some(target => src.includes(target))
}
function matchTargetWrapper(url) {
if (typeof url !== 'string') return
if (matchTarget(url)) {
onMatch(url)
}
}
const originalFetch = window.fetch
window.fetch = function (input, init) {
const url = typeof input === 'string' ? input : input.url
matchTargetWrapper(url)
return originalFetch.apply(this, arguments)
}
const originalXHROpen = XMLHttpRequest.prototype.open
XMLHttpRequest.prototype.open = function (method, url) {
matchTargetWrapper(url)
return originalXHROpen.apply(this, arguments)
}
const originalPushState = history.pushState
history.pushState = function (state, title, url) {
matchTargetWrapper(url)
return originalPushState.apply(this, arguments)
}
const originalReplaceState = history.replaceState
history.replaceState = function (state, title, url) {
matchTargetWrapper(url)
return originalReplaceState.apply(this, arguments)
}
let lastHref = location.href
setInterval(() => {
if (location.href !== lastHref) {
lastHref = location.href
matchTargetWrapper(location.href)
}
}, 300)
const originalOpen = window.open
window.open = function (url, ...rest) {
matchTargetWrapper(url)
return originalOpen.call(this, url, ...rest)
}
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.tagName === 'IFRAME') {
const src = node.src || node.getAttribute('src')
matchTargetWrapper(src)
}
}
}
})
observer.observe(document.body, { childList: true, subtree: true })
window.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('iframe').forEach(iframe => {
const src = iframe.src || iframe.getAttribute('src')
matchTargetWrapper(src)
})
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment