Last active
January 21, 2017 17:48
-
-
Save noromanba/5b53d98f46e3f5ad93c3c351a56b6269 to your computer and use it in GitHub Desktop.
replace iframe to anchor link for UserScript/Bookmarklet
This file contains 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
// ==UserScript== | |
// @name iframe2link | |
// @namespace http://noromanba.flavors.me | |
// @description replace iframe to anchor link for UserScript/Bookmarklet | |
// @include http://example.com/DIY | |
// @grant none | |
// @noframes | |
// @run-at document-end | |
// @version 2017.1.21.0 | |
// @homepage https://gist.github.com/noromanba/5b53d98f46e3f5ad93c3c351a56b6269 | |
// @downloadURL https://gist.github.com/noromanba/5b53d98f46e3f5ad93c3c351a56b6269/raw/iframe2link.user.js | |
// @contributor noromanba http://let.hatelabo.jp/noromanba/let/hJmep5bN5Ytu (Fork of) | |
// @contributor noromanba https://gist.github.com/noromanba/26d1804fac23f76d9dfc (accidental remove) | |
// @license MIT License https://nrm.mit-license.org/2016 | |
// @author noromanba http://noromanba.flavors.me | |
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/List-remove.svg/128px-List-remove.svg.png | |
// ==/UserScript== | |
// Icon (PD by The Tango! Desktop Project) | |
// https://commons.wikimedia.org/wiki/File%3AList-remove.svg | |
// Devel | |
// https://gist.github.com/noromanba/26d1804fac23f76d9dfc | |
//*/ | |
(() => { | |
'use strict'; | |
const whitelist = [ | |
'[src^="javascript:"]', | |
// e.g. Tumblr fixed position follow-teaser WTF | |
'[src^="about:blank"]', | |
// e.g. Autopagerize in Fx | |
'[src^="data:text/html"]', | |
'[src*="//www.facebook.com/plugins/"]', | |
// old? versioned endpoint; e.g. http://www.facebook.com/v2.3/plugins/ | |
'[src*="//www.facebook.com/v2."]', | |
// #twitter-widget-* .twitter-share-button .twitter-tweet-button | |
'[class|="twitter"]', | |
'[src^="//platform.twitter.com/widgets/"]', | |
'[src^="https://apis.google.com/"]', | |
'[src^="https://accounts.google.com/o/"]', | |
'[src*="//www.blogger.com/comment-iframe.g?"]', | |
'[src^="https://embed.tumblr.com/widgets/share/button"]', | |
'[src^="http://assets.tumblr.com/assets/"]', | |
'[src^="https://secure.assets.tumblr.com/"]', | |
'[src^="https://widgets.getpocket.com/"]' | |
].map(sel => ':not(' + sel + ')').join(''); | |
const replace = (ctx) => { | |
if (ctx.tagName.toLowerCase() === 'iframe') { | |
ctx = ctx.parentNode || document.body; | |
} | |
Array.from(ctx.querySelectorAll('iframe[src]' + whitelist), iframe => { | |
const link = document.createElement('a'); | |
link.href = iframe.src; | |
link.appendChild(document.createTextNode(link.href)); | |
iframe.parentNode.replaceChild(link, iframe); | |
}); | |
}; | |
replace(document.body); | |
new MutationObserver(records => { | |
records.forEach(record => { | |
const ctx = record.target; | |
if (!ctx.querySelectorAll) return; | |
replace(ctx); | |
}); | |
}).observe(document.body, { childList: true, subtree: true }); | |
})(); | |
// Bookmarklet | |
// http://userjs.up.seesaa.net/js/bookmarklet.html | |
/*/ | |
javascript:(()=>{const whitelist=['[src^="javascript:"]','[src^="about:blank"]','[src^="data:text/html"]','[src*="//www.facebook.com/plugins/"]','[src*="//www.facebook.com/v2."]','[class|="twitter"]','[src^="//platform.twitter.com/widgets/"]','[src^="https://apis.google.com/"]','[src^="https://accounts.google.com/o/"]','[src*="//www.blogger.com/comment-iframe.g?"]','[src^="https://embed.tumblr.com/widgets/share/button"]','[src^="http://assets.tumblr.com/assets/"]','[src^="https://secure.assets.tumblr.com/"]','[src^="https://widgets.getpocket.com/"]'].map(sel=>':not('+sel+')').join('');const replace=(ctx)=>{if(ctx.tagName.toLowerCase()==='iframe'){ctx=ctx.parentNode||document.body;}Array.from(ctx.querySelectorAll('iframe[src]'+whitelist),iframe=>{const link=document.createElement('a');link.href=iframe.src;link.appendChild(document.createTextNode(link.href));iframe.parentNode.replaceChild(link,iframe);});};replace(document.body);new MutationObserver(records=>{records.forEach(record=>{const ctx=record.target;if(!ctx.querySelectorAll)return;replace(ctx);});}).observe(document.body,{childList:true,subtree:true});})(); | |
//*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment