我正在尝试在浏览器上下载选定的字词或文字(mozilla或chrome)。当我选择一个单词并右键单击它时,我可以看到“将所选文本保存到文件”选项。但我无法使功能正常工作。它不会下载文本。我对延伸写作很新。我想通过简单的例子来学习东西
的manifest.json
{
"manifest_version":2,
"version":"1.0",
"name": "Selected Text Saver",
"permissions": [
"contextMenus",
"activeTab"
],
"background": {
"scripts": ["main.js"]
}
}
main.js
mouse1 = function(){
chrome.tabs.executeScript({
file: 'saver.js'
});
};
chrome.contextMenus.create({
title: "Save this selected text to file",
contexts:["selection"],
onclick: mouse1
});
saver.js
var text = window.getSelection().toString();
var bb = new Blob([text], {type: 'text/plain'});
var a = document.createElement('a');
a.download = document.title.slice(0, 50).replace('/*[<>:/\\|?*]*/g', '') + '.txt';
a.href = window.URL.createObjectURL(bb);
a.dataset.downloadurl = ['text/plain', a.download, a.href].join(':');
答案 0 :(得分:1)
JavaScript中的JavaScript不会对function buildDriveInventory($inventory)
{
$type = [1, 2, 4, 6, 8, 10, 12, 14, 16];
$unit = "lbs";
//create new array to hold values
$new_values = [];
// build up defaults
foreach ($type as $type_value) {
$new_values[$type_value.$unit] = 0;
}
// overwrite with actual inventory
foreach ($inventory as $product) {
if (in_array($product['capacity'], $type)) {
$new_values[$product['capacity'].$product['unit']] = $product['total'];
}
}
return $new_values;
}
后面的已创建<a>
元素执行任何进一步的任务。
将创建的a.dataset.downloadurl = ['text/plain', a.download, a.href].join(':');
元素附加到<a>
并调用document.body
,以便系统提示用户下载文件。