Either a userscript or a browser extension can do this. A Greasemonkey script will run on a page, irregardless of whether it's in an iframe (unless you tell it not to).
To block external links, compare each link's hostname
to the iframed page's hostname
.
Here's a complete Greasemonkey script that illustrates the process:
// ==UserScript==
// @name _Block cross-domain links
// @include http://www.puppylinux.com/*
// @grant GM_addStyle
// ==/UserScript==
//-- Only run if the page is inside a frame or iframe:
if (window.top !== window.self) {
var linkList = document.querySelectorAll ("a");
Array.prototype.forEach.call (linkList, function (link) {
if (link.hostname !== location.hostname) {
//-- Block the link
link.href = "javascript:void(0)";
}
} );
//-- Mark the links, so the user knows what's up.
GM_addStyle ( " \
a[href='javascript:void(0)'] { \
white-space: nowrap; \
cursor: default; \
} \
a[href='javascript:void(0)']::after { \
background: orange; \
content: 'X'; \
display: inline-block; \
margin-left: 0.3ex; \
padding: 0 0.5ex; \
} \
" );
}
You can install and run that script against this test page on jsFiddle.
Note: