拆解页面脚本和应用层脚本的强关联
This commit is contained in:
@@ -2,7 +2,26 @@
|
||||
利用chrome的fetch来避免跨域
|
||||
**/
|
||||
|
||||
import {getImageBlob} from "@/utils/image";
|
||||
/**
|
||||
* 根据图片URL获取Blob对象
|
||||
* @param imageUrl
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
function getImageBlob(imageUrl) {
|
||||
return new Promise((resolve) => {
|
||||
fetch(imageUrl).then((response) => response.blob()) // 将响应转换为Blob对象
|
||||
.then((blobData) => {
|
||||
const fileName = imageUrl.match(/\/([^/]+)$/).at(-1)
|
||||
const reader = new FileReader();
|
||||
// 读取Blob对象的内容
|
||||
reader.onloadend = function () {
|
||||
const image = {blobData, fileName}
|
||||
resolve({image});
|
||||
};
|
||||
reader.readAsArrayBuffer(blobData); // 将Blob对象作为参数传递给FileReader的readAsArrayBuffer()方法
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.type == 'api') {
|
||||
|
||||
Reference in New Issue
Block a user