在Chrome扩展程序中存储当前浏览器的源代码

时间:2019-07-25 16:54:01

标签: javascript html google-chrome-extension

我正在尝试通过chrome扩展程序获取当前浏览器的源代码(HTML代码)。我试图将此HTML代码存储在字符串中,但似乎不起作用。

http://jsfiddle.net/ufbxge08/2/

<head>
<style>
  button {
    height: 30px;
    width: 100px;
    outline: none;
  }
</style>
</head>
<body>
<button id="bth1">Predict</button>
<script src="popup.js"></script>
<script src="jquery-2.0.3.js"></script>
<form>
  <label id = 'mybtn1'></label>
</form>

JavaScript:

btn1.onclick = function scrapeThePage() {
// Keep this function isolated - it can only call methods you set up in content scripts
var htmlCode = document.documentElement.outerHTML;
var btn = document.getElementById("mybtn1");
btn.innerHTML = htmlCode;
console.log(htmlCode)

}

我希望代码将源代码存储在字符串中,但是似乎不起作用。

1 个答案:

答案 0 :(得分:0)

如果要在HTML元素内显示HTML代码,则可能需要设置文本内容(innerText),而不是HTML内容(innerHTML):

bth1.onclick = function scrapeThePage() {
    // Keep this function isolated - it can only call methods you set up in content scripts
    var htmlCode = document.documentElement.outerHTML;
    var btn = document.getElementById("mybtn1");
    btn.innerText = htmlCode;
}

http://jsfiddle.net/1jk94r50/