我正在尝试使用 chrome.getMediaGalleries
API 列出用户设备上的媒体库。它一直有效,直到 item.name
部分。
我观看了video来自 Google Developers youtube channel ,他们在其中使用了 JSON.parse
。然而,这会抛出一个 Uncaught SyntaxError: Unexpected token c in JSON atposition 0
正如预期的那样。这不是 JSON。
所以我向您提出的问题是:如何检索人类可读的内容(视频、音乐等)。这是到目前为止我的代码:
getGalleriesInfo(/*Array of DOMFileSystem objects*/ results) {
if (!results.length)
return console.log('No galleries found');
console.log('Gallery count: ' + results.length);
results.forEach((item, index) => {
let gallery = item.name;// JSON.parse(item.name) doesn't work
console.log(index, ' => ', gallery); // 0 => chrome-extension_hmnfbbnkjlgjgmlgpacnkkndfidbejpo_0:External_media_galleries-Default-hmnfbbnkjlgjgmlgpacnkkndfidbejpo-1, etc.
});
}
// I'm using a custom element (document.registerElement)
createdCallback() {
chrome.mediaGalleries.getMediaFileSystems({
interactive: 'if_needed'
}, this.getGalleriesInfo);
}
谢谢,
贾斯汀
编辑:item
的 Chrome 调试器 View
(来源:jtprogramming.com)
最佳答案
chrome.getMediaGalleries API 似乎已更改。要查找任何给定画廊的名称,您必须使用 getMediaFileSystemMetadata方法。
_getGalleriesInfo(results) {
if (!results.length)
return console.log('No galleries found');
console.log('Gallery count: ' + results.length);
results.forEach((item, index) => {
console.log(chrome.mediaGalleries.getMediaFileSystemMetadata(item));
});
}
它返回一个带有 DOMFileSystem 元数据的对象。
Object {
galleryId: (string) int,
isAvailable: boolean,
isMediaDevice: boolean,
isRemovable: boolean,
name: "C:\example\path\to\gallery"
}
关于javascript - 使用 chrome.mediaGalleries API 获取用户图库的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39621596/