在background.html

时间:2018-03-07 02:31:19

标签: javascript google-chrome-extension

background.js中,我注入了iframe

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.action == "doubanSearch")
            test(request.title, request.year);
    }
);

function test(title, year){
    var frame = document.createElement('iframe');
    frame.src = 'https://movie.douban.com/subject_search?cat=1002&search_text=' + title + ' '+ year;
    document.body.appendChild(frame);
  }

但是,加载iframe后,background.js不再响应doubanSearch请求。是否有一个解决方案允许background.js保持对未来请求的响应,即使加载了iframe

我已经单独检查了content.js并且可以确认它是否符合我的要求。

更新1

发出请求的netflixContent.js

var prevDOM = null;    
var prevMovieTitle = 'prevMovie';

// Mouse listener for any move event on the current document.
document.addEventListener('mousemove', function (e) {
    var srcElement = e.srcElement; 
    if (srcElement == null)
        return;

    if (prevDOM != srcElement){
        prevDOM = srcElement;    
        return;    
    }       

    // find the bob-overlay class    
    if (srcElement.parentElement != null && srcElement.parentElement.className.startsWith('bob')) {    
        while (srcElement.className!="bob-overlay"){    
            srcElement = srcElement.parentElement;    
            // if srcElement is no longer a bob- class, we are out of luck here.    
            if (srcElement == null || !srcElement.className.startsWith('bob'))    
                return;    
        }    
    }     

    // the srcElement at this stage has to be bob-overlay class!   
    if (srcElement == null || srcElement.className!="bob-overlay")    
        return;        

    // now we are in the right place, get movie title and publication year    
    var movieTitle = srcElement.getElementsByClassName('bob-title');    
    var movieYear = srcElement.getElementsByClassName('year');            

    if (movieTitle.length != 1){    
        console.log('Movie title not found.', srcElement);    
        return;    
    }

    if (movieYear.length != 1){    
        console.log('Movie year not found.', srcElement);    
        return;    
    }        

    // now get the title and year    
    movieTitle = movieTitle[0].textContent;    
    movieYear = movieYear[0].textContent.trim();

    // if the current movie is the same as the previous movie, we return.    
    if (movieTitle == prevMovieTitle)    
        return;

    // return if title is empty    
    if (movieTitle == '')    
        return;

    // if movie year isn't empty, add parenthesis.    
    if (movieYear != '')   
        movieYear = '(' + movieYear + ')';        

    prevMovieTitle = movieTitle;    
    console.log('Movie found:', movieTitle, movieYear);

    // replace special characters with space.    
    movieTitle = movieTitle.replace(/[^\w\s]/g, ' ').trim();    
    console.log('Movie found (special characters removed) :', movieTitle, movieYear);

    // now let's send the message and start searching!    
    chrome.runtime.sendMessage({action: 'doubanSearch', title: movieTitle, year: movieYear});                

}, false);

它是github上托管的开源,以防您需要查看整个代码。我对js和chrome扩展开发真的很陌生,所以请执行我糟糕的编码:)。

更新2

发送请求前的背景页面:

before request is sent

发送请求并加载iframe后的后台页面

after request is sent and iframe is loaded

此外,chrome:// extensions:

中不再提供后台网页链接

enter image description here

1 个答案:

答案 0 :(得分:1)

该网站执行框架破坏(将top.location分配给网页本身的网址),该网页将背景网页导航到网站网址,以便该网站不再包含后台网页。它看起来像Chrome中的疏忽,在最近的一些版本中已经间歇性地被阻止,并且即将在v67中被修复好。

解决方案是通过添加以下属性来沙箱iframe:

frame.sandbox = 'allow-scripts';

某些网站可能需要允许更多功能,例如allow-forms,有关完整列表,请参阅MDN