是否可以在我构建的扩展中获取我的网站 2,3 js 变量,以便我能够看到我构建的网站背后的信息
该扩展将帮助我开发我的网站
查看给定网站的变量(使用内容脚本)是可能的。只需注入您自己的内容脚本,并创建一个读取变量的脚本标记。由于内容脚本功能的某些限制,您无法使用这些变量,也无法在扩展程序上修改它们。您可以阅读以下文档与嵌入页面的通信.
例如,下面将读取网页中的 JS 变量并将其内容传输到后台页面,以便我们可以让我们的扩展处理它。您将在后台页面检查器中注意到变量已成功传递:
内容脚本.js
// JS script injection, so that we can read the JS class 'InternalJSVariable'
var postFriendMap = function() {
var textarea = document.getElementById('transfer-dom-area');
textarea.value = JSON.stringify(InternalJSVariable);
};
// Create a dummy textarea DOM.
var textarea = document.createElement('textarea');
textarea.setAttribute('id', 'transfer-dom-area');
textarea.style.display = 'none';
document.body.appendChild(textarea);
// Start injecting the JS script.
var script = document.createElement('script');
script.appendChild(document.createTextNode('(' + postFriendMap + ')();'));
document.body.appendChild(script);
// Inform our world that we have received the friend map data.
chrome.extension.sendRequest({internalVariable: textarea.value});
// Clean up since we no longer need this.
document.body.removeChild(textarea);
背景.html
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.internalVariable) {
var internal_object = JSON.parse(request.internalVariable);
console.log(internal_object );
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)