代码拉取完成,页面将自动刷新
同步操作将从 rubickCenter/rubick-doutu 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。