1 Star 1 Fork 2

李东平/rubick-doutu

forked from rubickCenter/rubick-doutu 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
preload.js 11.02 KB
一键复制 编辑 原始数据 按行查看 历史
muwoo 提交于 2021-12-10 17:32 . init
const electron = require('electron')
const { clipboard } = require('electron')
const app = electron.app
const path = require('path')
const http = require('http');
const https = require('https');
const fs = require('fs');
const os = require('os');
const nativeImage = require('electron').nativeImage
copyImg = (imgUrl, ctrlV) => {
if (os.type() == 'Windows_NT') {
// window 系统下载网络图片到 temp 目录,然后 以 html 写入clipboard
var the_dir = rubick.getPath('temp') + 'rubickDoutuPlugin/';
var path = the_dir + 'tempImage' + (new Date()).valueOf() + '.';
//检查临时目录并创建
fs.exists(the_dir, function (exists) {
if (!exists) {
fs.mkdir(the_dir, err => {
})
}
});
fetch(imgUrl)
.then(respone => respone.blob()) // 将响应体转换成blob格式数据
.then(blob => {
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function () {
let base64Data = reader.result;
var dataHeader = /^data:image\/(\w+);base64,/gim;
var imageType = 'gif';
while (re = dataHeader.exec(base64Data)) {
imageType = re[1];
break;
}
// 过滤data:URL
base64Data = base64Data.replace(/^data:.+;base64,/, '');
var dataBuffer = new Buffer.from(base64Data, 'base64');
path = path + imageType;
if (rubick.copyImage(dataBuffer) == false) {
console.log('clipboard.write')
fs.writeFile(path, dataBuffer, err => {
//windows 系统以 html 写入clipboard
clipboard.write({
html: "<img src=\"" + path + "\" width=\"200\" alt=\"" + path + "\" >",
})
});
} else {
console.log('rubick.copyImage')
}
rubick.hideMainWindow()
if (ctrlV == true) {
rubick.simulateKeyboardTap('v', rubick.isMacOs() ? 'command' : 'ctrl')
}
};
})
.catch(console.error);
} else if (os.type() == 'Darwin') {
// mac 系统下载网络图片到 temp 目录,然后以 NSFilenamesPboardType 的 Buffer 写入clipboard
var the_dir = rubick.getPath('temp') + 'rubickDoutuPlugin/';
// var path = the_dir + 'temp.' + imgUrl.substring(imgUrl.lastIndexOf(".") + 1)
var path = the_dir + 'tempImage' + (new Date()).valueOf() + '.';
//检查临时目录并创建
fs.exists(the_dir, function (exists) {
if (!exists) {
fs.mkdir(the_dir, err => {
})
}
});
fetch(imgUrl)
.then(respone => respone.blob()) // 将响应体转换成blob格式数据
.then(blob => {
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function () {
let base64Data = reader.result;
var dataHeader = /^data:image\/(\w+);base64,/gim;
var imageType = 'gif';
while (re = dataHeader.exec(base64Data)) {
imageType = re[1];
break;
}
if (imageType != 'gif') {
rubick.copyImage(base64Data)
} else {
// 过滤data:URL
base64Data = base64Data.replace(/^data:.+;base64,/, '');
var dataBuffer = new Buffer.from(base64Data, 'base64');
path = path + imageType;
console.log(path)
fs.writeFile(path, dataBuffer, err => {
// rubick.copyImage(path)
// clipboard.writeBuffer(
// 'NSFilenamesPboardType',
// Buffer.from(`
// <?xml version="1.0" encoding="UTF-8"?>
// <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
// <plist version="1.0">
// <array>
// <string>`+ path + `</string>
// </array>
// </plist>
// `)
// )
// fs.readFile(path, (err, data22) => {
// if (err) {
// console.log('xxx222')
// } else {
// let buffer22 = new Buffer.from(data22, "utf-8");
// aa = clipboard.writeBuffer(
// "public.utf8-plain-text",
// buffer22
// );
// console.log(aa)
// console.log('xxx')
// }
// });
// clipboard.writeBuffer('NSFilenamesPboardType', Buffer.from(plist.build(path)));
rubick.copyFile(path)
});
}
rubick.hideMainWindow()
if (ctrlV == true) {
rubick.simulateKeyboardTap('v', rubick.isMacOs() ? 'command' : 'ctrl')
}
};
})
.catch(console.error);
} else if (os.type() == 'Linux') {
var the_dir = rubick.getPath('temp') + '/rubickDoutuPlugin/';
var path = the_dir + 'tempImage' + (new Date()).valueOf() + '.';
//检查临时目录并创建
fs.exists(the_dir, function (exists) {
if (!exists) {
fs.mkdir(the_dir, err => {
})
}
});
fetch(imgUrl)
.then(respone => respone.blob()) // 将响应体转换成blob格式数据
.then(blob => {
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function () {
let base64Data = reader.result;
var dataHeader = /^data:image\/(\w+);base64,/gim;
var imageType = 'gif';
while (re = dataHeader.exec(base64Data)) {
imageType = re[1];
break;
}
// 过滤data:URL
base64Data = base64Data.replace(/^data:.+;base64,/, '');
var dataBuffer = new Buffer.from(base64Data, 'base64');
path = path + imageType;
fs.writeFile(path, dataBuffer, err => {
rubick.copyFile(path)
});
rubick.hideMainWindow()
if (ctrlV == true) {
rubick.simulateKeyboardTap('v', rubick.isMacOs() ? 'command' : 'ctrl')
}
};
})
.catch(console.error);
} else {
rubick.showNotification('当前系统暂不支持', clickFeatureCode = null, silent = false)
}
}
copyImg2 = (imgUrl, ctrlV) => {
var the_dir = rubick.getPath('temp') + '/rubickDoutuPlugin/';
var path = the_dir + 'tempImage' + (new Date()).valueOf() + '.';
//检查临时目录并创建
fs.exists(the_dir, function (exists) {
if (!exists) {
fs.mkdir(the_dir, err => {
})
}
});
fetch(imgUrl)
.then(respone => respone.blob()) // 将响应体转换成blob格式数据
.then(blob => {
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function () {
let base64Data = reader.result;
// var dataHeader = /^data:image\/(\w+);base64,/gim;
// var imageType = 'gif';
// while (re = dataHeader.exec(base64Data)) {
// imageType = re[1];
// console.log(imageType)
// break;
// }
// 过滤data:URL
// base64Data = base64Data.replace(/^data:.+;base64,/, '');
// var dataBuffer = new Buffer.from(base64Data, 'base64');
// path = path + imageType;
// fs.writeFile(path, dataBuffer, err => {
// rubick.copyFile(path)
// });
console.log(base64Data)
aaa = rubick.copyImage(base64Data)
console.log(aaa)
rubick.hideMainWindow()
if (ctrlV == true) {
rubick.simulateKeyboardTap('v', rubick.isMacOs() ? 'command' : 'ctrl')
}
};
})
.catch(console.error);
}
/**
* 清理缓存的图片
*/
cleanTempImageCahce = () => {
if (os.type() == 'Linux') {
var theDir = rubick.getPath('temp') + '/rubickDoutuPlugin/';
} else {
var theDir = rubick.getPath('temp') + 'rubickDoutuPlugin/';
}
fs.exists(theDir, function (exists) {
if (exists) {
let theFiles = [];
let files = fs.readdirSync(theDir);
files.forEach(function (item, index) {
let fPath = path.join(theDir, item);
let stat = fs.statSync(fPath);
if (stat.isFile() === true) {
theFiles.push(fPath);
}
});
if (theFiles.length >= 20) {
for (fiel_v of theFiles) {
fs.unlink(fiel_v, function (error) {
if (error) {
console.log(error);
return false;
}
})
}
}
}
});
}
matchImgUrl = str => {
var reg = /data-original="(.*?)"/gim;
var res = []
while (re = reg.exec(str)) {
res.push(re[1])
}
return res;
}
matchImgUrl_6 = str => {
// var reg = /<a\Wclass="col-sm-3".*<img\Wsrc="(.*)"\Wtitle=/gim;
var reg = /img src="(.*)" title/gim;
var res = []
while (re = reg.exec(str)) {
res.push(re[1])
}
return res;
}
matchImgUrl_7 = str => {
// var reg = /<a\Wclass="col-sm-3".*<img\Wsrc="(.*)"\Wtitle=/gim;
console.log(str)
var reg = /data-src="(.*)" alt/gim;
var res = []
while (re = reg.exec(str)) {
res.push(re[1])
}
return res;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ibm317/rubick-doutu.git
git@gitee.com:ibm317/rubick-doutu.git
ibm317
rubick-doutu
rubick-doutu
master

搜索帮助