|
| 1 | +"use strict"; |
| 2 | +let app = WizExplorerApp; |
| 3 | +// let wizWindow = app.Window; |
| 4 | +// let objBrowser = wizWindow.CurrentDocumentBrowserObject |
| 5 | +let objWindow = app.Window; |
| 6 | +let objDocument = objWindow.CurrentDocument; |
| 7 | +let objCommon = app.CreateWizObject("WizKMControls.WizCommonUI"); |
| 8 | + |
| 9 | +// paths |
| 10 | +let tempPath = objCommon.GetSpecialFolder("TemporaryFolder"); |
| 11 | +let documentTempPath = tempPath + objDocument.GUID + '/128/'; // e.g. F:\Richex\WizData\temp\209c9d6f-66c7-41eb-a944-670d92c7a2f8/128/ |
| 12 | +let documentTempIndexFilesPath = documentTempPath + 'index_files/'; // e.g. F:\Richex\WizData\temp\209c9d6f-66c7-41eb-a944-670d92c7a2f8/128/index_files/ |
| 13 | + |
| 14 | + |
| 15 | +let imagePaths = []; |
| 16 | + |
| 17 | +let html = objDocument.GetHtml(); |
| 18 | +let newHtml; |
| 19 | +console.log(html); |
| 20 | + |
| 21 | +// Markdown |
| 22 | +let MarkdownRegex = /(!\[[^\[]*?\]\()(.+?)(\s+['"][\s\S]*?['"])?(\))/g; |
| 23 | +newHtml = html.replace(MarkdownRegex, (whole, a, b, c, d) => { |
| 24 | + let src = b; |
| 25 | + if (isHttpSrc(b)) src = convertImgSrctoLocal(b); |
| 26 | + imagePaths.push(buildImageTag(documentTempPath + src)); |
| 27 | + return a + src + (c || '') + d; |
| 28 | +}); |
| 29 | + |
| 30 | +// html |
| 31 | +let HtmlRegex = /<img.*src=['"](.*?)['"].*?\/?>/gi; |
| 32 | +newHtml = newHtml.replace(HtmlRegex, (whole, a) => { |
| 33 | + let src = a; |
| 34 | + if (isHttpSrc(a)) src = convertImgSrctoLocal(a); |
| 35 | + |
| 36 | + imagePaths.push(buildImageTag(documentTempPath + src)); |
| 37 | + return src; |
| 38 | +}); |
| 39 | + |
| 40 | +console.log(imagePaths); |
| 41 | +let uniq = _uniq(imagePaths); |
| 42 | +console.log(uniq); |
| 43 | + |
| 44 | +newHtml = newHtml.replace('</body>', '<picture_convert style="display: none;">' + uniq.join('') + '</picture_convert></body>'); |
| 45 | +console.log(newHtml); |
| 46 | + |
| 47 | +objDocument.UpdateDocument3(newHtml, 0); |
| 48 | +alert('所有网络图片已经下载并转换到本地!'); |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +function convertImgSrctoLocal(src) { |
| 53 | + if (!isHttpSrc(src)) return src; |
| 54 | + |
| 55 | + let ext = objCommon.ExtractFileExt(src); |
| 56 | + let saveName = objCommon.URLDownloadToTempFile(src); // e.g. C:\Users\Richex\AppData\Local\Temp\Wiz\c9c3aace-6b3a-402d-b539-85bc3821d006.tmp |
| 57 | + let filename = objCommon.ExtractFileTitle(saveName) + ext; |
| 58 | + let newName = documentTempIndexFilesPath + filename; |
| 59 | + objCommon.CopyFile(saveName, newName); |
| 60 | + return 'index_files/' + filename; |
| 61 | +} |
| 62 | + |
| 63 | +function isHttpSrc(src) { |
| 64 | + return /^https?:\/\//i.test(src); |
| 65 | +} |
| 66 | + |
| 67 | +function buildImageTag(src) { |
| 68 | + return '<img src="' + src + '">'; |
| 69 | +} |
| 70 | + |
| 71 | +function _uniq(arr) { |
| 72 | + let _arr = []; |
| 73 | + for (let item of arr) { |
| 74 | + if (!~_arr.indexOf(item)) _arr.push(item); |
| 75 | + } |
| 76 | + return _arr; |
| 77 | +} |
| 78 | + |
| 79 | +// var pluginPath = app.GetPluginPathByScriptFileName('index.js') |
| 80 | +// var path = pluginPath + 'content.js' |
| 81 | + |
| 82 | +// wizBrowser.ExecuteScriptFile(path, res => { |
| 83 | +// wizBrowser.ExecuteFunction2('PictureLocalInit', app, wizBrowser) |
| 84 | +// }) |
| 85 | + |
| 86 | +// wizBrowser.ExecuteScript('document.body.innerHTML', text => { |
| 87 | +// alert(text); |
| 88 | +// console.log(document) |
| 89 | +// }) |
| 90 | + |
| 91 | +// wizBrowser.ExecuteScript('console.log(document.body.innerHTML)', doc => { |
| 92 | +// wizBrowser.ExecuteScript('') |
| 93 | +// }) |
0 commit comments