javascript - 将当前浏览器的源代码存储在 chrome 扩展中
问题描述
我正在尝试通过 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)
}
我希望代码将源代码存储在一个字符串中,但它似乎不起作用。
解决方案
如果您想在 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;
}
推荐阅读
- python - How to apply clipping to trainable variables in TensorFlow
- php - 我们如何使用 eloquent 在 laravel 5.4 中选择和更新表?
- selenium - Selenium 中的 DOM 属性值
- angular - 角度-primeng-确认对话框不起作用
- android - 片段已泄漏,尽管已附加
- asp.net - 如果我向网络服务器发出请求,网络服务不会第二次发送数据
- python-datetime - 将数据框的列转换为时间戳
- google-apps-script - Google GroupsApp 中的 .getMembers() 和 .getGroups() 列出了意外结果
- laravel - Laraven Cron 表达式
- javascript - 通过 addEventListener 添加的事件监听器被多次调用