Greasemonkey IFRAME删除<div>

时间:2015-12-03 18:34:57

标签: javascript jquery iframe greasemonkey

这是一个paintmoneky脚本,弹出将内容链接到iframe窗口。我知道有一种方法可以删除特定div或除去我想要的所有div:

$("preview").contents().find("*:not(#content container)").remove();

这里我想要内容容器在iframe中显示。但我不知道如何从greasemonkey中调用它。 iframe窗口位于同一个域中。这是脚本本身。它不是我的,只是在网上发现它并试图使它在我的情况下有用

// ==UserScript==
// @name           Hover Preview
// @namespace      HP
// @description    Pops up a floating div when you hover over a link, containing the target page!
// @include        *
// @grant       GM_addStyle
// @version 0.0.1.20150213035046
// ==/UserScript==

// TODO:
// Don't act if the target is a file-type.  i.e. we don't want to be prompted
// to save a zip file just because we hovered on it.
// KNOWN UNFIXABLE BUG:
// Damnit some pages break out of the iframe!  Don't try to use this on
// StackOverflow links!

// if (window.document != document) {
    // return; // Don't run in iframes
// }

// Quite nice on apache file listings of .jpegs, but a bit slow.  Ideally pre-load hoverable images?
// Could be a bit heavy.  It depends on the page...
// A different bookmarklet to turn all "links to images" into "images" would be nice. :)

var focusReactionTime = 300;
var unfocusReactionTime = 300;

var focus = undefined;
var lastFocus = undefined;
var timer = null;

var myPopup;
var myFrame;

var isOverPopup = false;

function checkFocus() {
    if (focus) {
        showPreviewWindow(focus);
    }
}

function eekAMouse(evt) {
    if (evt.currentTarget.tagName !== "A") {
        return;
    }
    if (!focus) {
        focus = evt.currentTarget;
        if (myFrame && focus.href && myFrame.href == focus.href) {
            showPreviewWindow(focus,evt);
        } else {
            if (timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(checkFocus,focusReactionTime);
        }
    } else {
        window.status = "Already focused on a link wtf!";
    }
}

function phewMouseGone(evt) {
    if (evt.currentTarget.tagName !== "A") {
        return;
    }
    focus = undefined;
    if (timer) {
        clearTimeout(timer);
    }
    // TESTING: Don't hide the popup if mouse is currently over the popup!
    timer = setTimeout(clearPopup,unfocusReactionTime);
}

function clearPopup(e) {
    if (isOverPopup || focus)
        return;
    if (myPopup) {
        myPopup.style.display = 'none';
    }
}

// DONE: If the user clicks a link, this isn't really a hover, so we should not
// activate and just let the user's click be processed!
function aClick(evt) {
    focus = undefined;
}

function createPopup() {
    // Create frame
    myPopup = document.createElement('DIV');
    /** Seems style does not work for Konqueror this way. **/
    myPopup.innerHTML =
        "<STYLE type='text/css'> iframe.preview { color: #ff8822; background-color: #ff0000; margin: 0px; padding: 2px; border: 2px solid white; text-align: center; } </STYLE>"
        +
        "<IFRAME class='preview' width='"+(window.innerWidth*0.35)+"' height='"+(window.innerHeight*0.35)+"' src='about:blank' onload='doSomething()'></IFRAME>";
    myPopup.addEventListener("mouseover", function(evt) { isOverPopup=true; }, false);
    myPopup.addEventListener("mouseout", function(evt) { isOverPopup=false; setTimeout(clearPopup,unfocusReactionTime); }, false);
    document.documentElement.appendChild(myPopup);
    myPopup.style.position = "fixed";
    myPopup.style.right = "12px";
    myPopup.style.bottom = "12px";
    myPopup.style.zIndex = "10000";
    myFrame = myPopup.getElementsByTagName('IFRAME')[0];
}
function doSomething(){
}
function showPreviewWindow(link,evt) {
    if (!myFrame) {
        createPopup();
    }
    myPopup.style.display = '';
    if (!myFrame.src || myFrame.src != link.href)
        myFrame.src = link.href;
$("preview").contents().find("*:not(#adPage__content container_25)").remove();
}

function init() {
    for (var i=0;i<document.links.length;i++) {
        var link = document.links[i];
        /** The new way: **/
        link.addEventListener("mouseover", eekAMouse, false);
        link.addEventListener("mouseout", phewMouseGone, false);
        link.addEventListener("click", aClick, false);
    }
}

init();

0 个答案:

没有答案