4 javascript google-chrome-extension content-security-policy
我正在创建一个隐私扩展,该扩展在 document_start 上运行内容脚本。
内容脚本需要为每个不同的来源注入一个具有动态值的脚本,例如 google.com、twitter.com 等。
这是我的内容脚本:
console.log("Content Script Running ...");
console.log("Window origin: " + window.location.href);
function inject(filePath) {
var script = document.createElement('script');
script.src = chrome.extension.getURL(filePath);
script.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
}
function injectText(text) {
var script = document.createElement('script');
script.textContent = text;
script.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
}
function getSeed(origin) {
// Get a Storage object
var storage = window.sessionStorage;
// Do we already have a seed in storage for this origin or not?
var seed = storage.getItem(origin);
if (seed === null) {
// Initialise a 32 byte buffer
seed = new Uint8Array(32);
// Fill it with cryptographically random values
window.crypto.getRandomValues(seed);
// Save it to storage
storage.setItem(origin, seed);
}
return seed;
}
var origin = window.location.hostname;
var seed = getSeed(origin);
injectText("var seed = '" + seed + "';");
console.log("[INFO] Injected Seed ...");
inject("js/lib/seedrandom.min.js");
console.log("[INFO] Injected Seed Random ...");
inject("js/random.js");
console.log("[INFO] Injected Random ...");
inject("js/api/document.js");
console.log("[INFO] Injected Document API ...");
inject("js/api/navigator.js");
console.log("[INFO] Injected Navigator API ...");
inject("js/api/canvas.js");
console.log("[INFO] Injected Canvas API ...");
inject("js/api/history.js");
console.log("[INFO] Injected History API ...");
inject("js/api/battery.js");
console.log("[INFO] Injected Battery API ...");
inject("js/api/audio.js");
console.log("[INFO] Injected Audio API ...");
inject("js/api/element.js");
console.log("[INFO] Injected Element API ...");
Run Code Online (Sandbox Code Playgroud)
当尝试在具有严格 CSP(例如 github.com)的网站上运行此扩展时,具有动态种子值的脚本被阻止,并且依赖于该值的其他脚本最终引用未定义的值。有什么想法可以解决这个问题。
使用 src 属性加载的脚本是可以的,因为它们位于 .js 文件中,并且是从扩展加载的,但是具有动态值(又名 var Seed = ...)的脚本会被阻止,因为它是使用 textContent 属性注入的。
我需要让这段代码在页面上的任何其他脚本运行之前同步运行,因此我让内容脚本在 document_start 上运行。
有任何想法吗?
小智 5
我解决了这个问题。我遇到的主要问题是尝试注入具有以下内容的内联文本脚本:
var seed = $(value that changes depending on the page)
Run Code Online (Sandbox Code Playgroud)
这会被某些具有限制性内容安全策略的网站(例如 twitter.com 和 github.com)阻止。
我的解决方案是在我的内容脚本中执行以下操作:
var filePath = // Get filepath to script
var seed = // Get seed value in content script
var script = document.createElement('script');
script.setAttribute("data-seed", seed);
script.src = chrome.extension.getURL(filePath);
script.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
Run Code Online (Sandbox Code Playgroud)
这将在页面中创建一个脚本,如下所示
<script data-seed="$(DATA-SEED-VALUE)" src="$(SRC-VALUE)"></script>
Run Code Online (Sandbox Code Playgroud)
然后从现在作为页面脚本运行的脚本中(在网页的内容中):
var seed = document.currentScript.getAttribute("data-seed");
Run Code Online (Sandbox Code Playgroud)
从而获得种子。此解决方案更加简洁、简单,并且不需要更改 CSP,否则可能会给您正在交互的站点带来安全问题。
| 归档时间: |
|
| 查看次数: |
1454 次 |
| 最近记录: |