处理Chrome扩展程序中的数据URI获取事件

时间:2019-03-09 13:55:42

标签: google-chrome-extension event-handling data-uri

在Javascript中,您可以创建一个具有数据URI作为HREF的锚,并以编程方式单击链接以“下载”数据。在Chrome中可以正常使用。 Chrome扩展程序可以通过将webRequest.onBeforeRequest与“ all_urls”过滤器挂钩来拦截从网络获取内容。这对于正常的HTTP请求正常工作,但对编程数据URI单击而言不适用于我。 我的问题是,这是一个错误/将来的功能(也许是this)吗?或者是否还有其他接口可以用来挂接数据URI访问?

这是我的Javascript测试代码:

<html>
<head>
<script lang="javascript">
function download(filename, text)
{
	var element = document.createElement('a');
	element.setAttribute(
	  'href',
	  'data:text/plain;charset=utf-8,' + encodeURIComponent(text)
	);
	element.setAttribute('download', filename );
	element.style.display = "none";
	document.body.appendChild(element);
	element.click();
	document.body.removeChild(element);
}

function go()
{
	download("hello.txt","Hi there");
}
</script>
</head>
<body onload="go();">
	Testing
</body>
</html>

这是我的Chrome扩展程序代码:

function checker(details)
{
	var bkg = chrome.extension.getBackgroundPage();
	bkg.console.log( JSON.stringify(details));
}

chrome.webRequest.onBeforeRequest.addListener( 
  checker, 
  {urls: ["<all_urls>"]}, 
  ["blocking"] 
);

0 个答案:

没有答案