Skip to content

Instantly share code, notes, and snippets.

@maple3142
Last active August 26, 2019 14:03
Show Gist options
  • Select an option

  • Save maple3142/57bb7506832fc416dc0344a649448c73 to your computer and use it in GitHub Desktop.

Select an option

Save maple3142/57bb7506832fc416dc0344a649448c73 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Twitter test + sentineljs
// @namespace maple-test
// @match https://twitter.com/*
// @require https://cdnjs.cloudflare.com/ajax/libs/sentinel-js/0.0.5/sentinel.umd.js
// @grant none
// ==/UserScript==
const added=new WeakMap()
function addBtn(img){
if(!img || added.get(img))return
const btn=document.createElement('button')
btn.onclick=()=>window.open(img.src, '_blank')
btn.textContent='View Image in new tab'
img.parentElement.appendChild(btn)
added.set(img, true)
}
sentinel.on('div[aria-labelledby="modal-header"] img[src^="https://pbs.twimg"]', addBtn)
// ==UserScript==
// @name Twitter test
// @namespace maple-test
// @match https://twitter.com/*
// @grant none
// ==/UserScript==
const added=new WeakMap()
function addBtn(img){
if(!img || added.get(img))return
const btn=document.createElement('button')
btn.onclick=()=>window.open(img.src, '_blank')
btn.textContent='View Image in new tab'
img.parentElement.appendChild(btn)
added.set(img, true)
}
const selectModalImgs=()=>Array.from(document.querySelectorAll('div[aria-labelledby="modal-header"] img[src^="https://pbs.twimg"]'))
new MutationObserver(()=>{
const imgs=selectModalImgs()
if(imgs.length){
imgs.forEach(addBtn)
}
}).observe(document.body, {subtree:true, childList: true})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment