我正在尝试使用Chrome扩展程序在网站上下载一系列文件。但是,在第一次下载后,javascript注入无法正常工作。
以下代码是扩展程序的精简版本,可从此页面下载文件:
http://spreadsheetpage.com/index.php/file/word_clock/
manifest.json文件:
{
"manifest_version": 2,
"name": "File downloader",
"description": "File downloader",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"activeTab",
"http://*/*",
"https://*/*"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
popup.html文件:
<!doctype html>
<html>
<head>
<title>File downloader</title>
<script src="popup.js"></script>
</head>
<body>
File downloader
</body>
</html>
popup.js文件:
function printamessagelater() {
console.log("Executed after download initiated");
chrome.tabs.executeScript({code: "console.log('Executed after download initiated');"});
}
console.log("Starting");
chrome.tabs.executeScript({code: "console.log('Starting');"});
javascriptinjection = "document.evaluate(" + String.fromCharCode(34) + "//a[text()='word clock.xlsm']" + String.fromCharCode(34) + ",document,null,9,null).singleNodeValue.click();";
chrome.tabs.executeScript({code: javascriptinjection});
setTimeout(function(){printamessagelater()}, 2000);
我看到的行为是chrome.tabs.executeScript({code: "console.log('Executed after download initiated');"});
行不会导致打印到页面控制台的任何内容。
我的猜测是下载会以某种方式改变焦点/上下文/执行环境(我并不真正理解),因此javascript没有被注入。
非常感谢任何帮助。