2015-10-17 27 views
0

該問題繼續下面給出的鏈接。整個頁面的源代碼,包括鉻擴展中的框架

Getting the source HTML of the current page from chrome extension

在上述URL中給出的答案頂部得到本標籤的源代碼。但是這並沒有在源代碼的iframe中獲得源代碼。

如何訪問整個頁面的源代碼,包括chrome擴展中的FRAMES?

+0

歡迎堆棧溢出。我們很樂意幫助你。爲了提高獲得答案的機會,以下是一些提示:[我如何提出一個好問題?] –

回答

2

使用allFrames: true參數chrome.tabs.executeScript將代碼注入到所有幀中。回調函數將收到的結果的數組,所有的幀:

popup.js:

chrome.tabs.executeScript({ 
    allFrames: true, 
    code: 'JSON.stringify({ \ 
       url: location.href, \ 
       html: document.documentElement.innerHTML \ 
      })' 
}, function(results) { 
    if (chrome.runtime.lastError || !results || !results.length) { 
     console.log("Some error occurred", chrome.runtime.lastError); 
    } else { 
     results.forEach(function(str, index) { 
      var info = JSON.parse(str); 
      console.log("Frame #%d | %s | %s", 
       index, info.url, info.html.substr(0, 200) + "..."); 
     }); 
    } 
}); 
+0

工作就像一個魅力。謝謝。爲了避免混淆,在executeScript函數中添加可選的IntegerId參數 –

+0

使用chrome。* API函數,不需要爲* optional *參數指定'null'或'undefined'。這是一件非常好的事情。 – wOxxOm

+0

有沒有辦法從數組中獲取幀的url? –