【问题标题】:background.js is no longer accessible after iframe is loaded in background.html在 background.html 中加载 iframe 后,无法再访问 background.js
【发布时间】:2018-03-07 23:51:01
【问题描述】:

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);
  }

但是,一旦加载了 iframebackground.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

请求发送前的后台页面:

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

此外,chrome://extensions 中不再提供背景页面链接:

【问题讨论】:

  • @wOxxOm 谢谢,我已经用代码制作请求更新了这个问题。我已经尝试调试请求代码,它做得很好,我的意思是它继续发送请求。当 iframe 加载时 background.js 消失,并显示 iframe 源代码和 content.js。
  • 显示截图。
  • @wOxxOm 截图已添加。

标签: javascript google-chrome-extension


【解决方案1】:

站点执行 frame-busting(将 top.location 分配给页面本身的 URL),它将背景页面导航到站点 URL,因此扩展程序不再具有背景页面。这似乎是 Chrome 中的一个疏忽,在最近的一些版本中已被间歇性地阻止,并且即将在 v67 中得到修复。

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

frame.sandbox = 'allow-scripts';

某些网站可能需要更多功能才能被允许,例如allow-forms,完整列表见MDN

【讨论】:

  • 谢谢,我必须添加参数allow-same-origin 否则它会抱怨并且不会加载页面。现在我有一个新问题。我似乎无法获取iframe 中的内容。我应该给content.js 什么权限? iframe 源 url 不起作用。
  • 确保你的内容脚本声明有"all_frames": true (example) 否则我不知道你对get the contents做了什么。
猜你喜欢
  • 1970-01-01
  • 2014-09-18
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
相关资源
最近更新 更多