Skip to content

Commit 6f18ecf

Browse files
committed
v1.0.0
1 parent 48cbd86 commit 6f18ecf

8 files changed

+195
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sublime-workspace

Picture.Localize.wizplugin

1.56 KB
Binary file not shown.

README.assets/20190417_140250.png

8.52 KB
Loading

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 开发文档
2+
3+
### 参考链接
4+
5+
[4.4 版本插件开发API文档](http://www.wiz.cn/manual/plugin/)
6+
7+
[4.5 版本插件开发API文档](http://www.wiz.cn/plugin-api-document-45.html)
8+
9+
10+
11+
### 文件说明
12+
13+
`/Picture.Localize.wizplugin` 为插件文件,将 `/src` 下的文件打包成 `zip` 并更名为 `wizplugin` 即可。
14+
15+
16+
17+
# 插件介绍
18+
19+
### 插件名称
20+
21+
PictureLocalize(图片本地化)
22+
23+
24+
25+
### 插件位置
26+
笔记顶部工具栏的**本地化网络图片**按钮,如下图:
27+
28+
![20190417_140250](README.assets/20190417_140250.png)
29+
30+
31+
32+
### 使用方式
33+
34+
在笔记是**非编辑**的状态下,点击工具栏中的**本地化网络图片**按钮,稍等片刻后会出现**所有网络图片已经下载并转换到本地!**的提示。
35+
36+
兼容4.5以后的客户端版本,插件调试开发的环境是 `4.11.18` 版本。
37+
38+
39+
40+
- 考虑到编辑器很多,无法做到一一兼容,所以请在笔记是**非编辑**模式下使用该插件
41+
- 点击按钮后的等待时间视网络情况不同
42+
- 出现提示后即已更新笔记,会自动刷新,如需还原可通过**历史版本**功能恢复
43+
- 兼容任意格式的笔记(Markdown 和 HTML),插件只会处理网络图片资源,笔记中引入的本地图片不会二次处理,放心使用!
44+
45+
46+
47+
### 插件用途
48+
49+
为笔记提供图片本地化功能,将笔记中所有的网络图片下载至笔记本地并替换,主要是为了解决以下几个问题:
50+
51+
1. 预防笔记图片为网络图片时,在离线环境下无法查看
52+
2. 防止网络资源服务器关闭服务等无法访问的情况下,图片无法正常打开的问题
53+
54+
55+
56+
将所有图片下载并转换为笔记自带图片,有效的预防了上述可能遇到的问题,同时也可以很方便的进行导出等工具(解压ziw文件)
57+
58+
59+
60+
### 其他说明
61+
62+
因为个人在保存文档的过程中,遇到一些笔记在保存后仍然使用网络图片的情况(例如粘贴内容到Wiz.Editor.md编辑器中等),为了防止网络图片资源失效,特做了一个小插件以解决该问题,将图片保存至笔记中。
63+
64+
插件是以4.5以后的版本调试开发的,因为4.5版本前后的客户端插件开发方式不同,所以不保证4.5之前的客户端兼容性。
65+
66+
顺便的确想吐槽一些开发文档确实是有点乱,建议官方优化一下,这样也好让更多开发者为Wiz的插件生态做贡献不是吗?
67+

src/index.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
// })

src/plugin.ini

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Common]
2+
FolderName=Picture.Localize
3+
AppName=PictureLocalize
4+
AppName_2052=本地化网络图片
5+
AppName_1028=本地化网络图片
6+
AppGUID={322ee635-82f5-4511-988d-43835ded612a}
7+
AppType=Tool
8+
AppVersion=1.0
9+
PluginCount=1
10+
SupportVersion=2
11+
12+
[Plugin_0]
13+
MenuType=NoteTools
14+
Caption=PictureLocalize
15+
Caption_2052=本地化网络图片
16+
Caption_1028=本地化网络图片
17+
GUID={4926b51c-885f-4df3-a6ae-98d827623d5f}
18+
Type=ExecuteScript
19+
ScriptFileName=index.min.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"folders":
3+
[
4+
{
5+
"path": "."
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)