外掛程式
外掛程式
外掛程式為 Word、Excel、PowerPoint 與 PDF 編輯器加入自己的工具:編輯器側邊欄中的面板、編輯器自帶「外掛程式」索引標籤中的按鈕、右鍵選單項目、使用者輸入時出現在游標處的建議,或在背景執行的頁面。它只能透過自動化 API 存取文件。
外掛程式套件
外掛程式是一個透過 https 提供的資料夾:mroffice-plugin.json(資訊清單)、面板頁與背景頁、指令碼、樣式與圖示。資訊清單提到的每個檔案都在這個資料夾內。
{
"manifestVersion": 1,
"id": "com.example.qr-code",
"name": "QR code",
"version": "1.0.0",
"description": "Insert a QR code",
"icon": "icon.svg",
"editors": ["docx", "pptx"],
"permissions": ["read", "write"],
"connect": ["https://api.example.com"],
"panel": { "url": "index.html", "title": "QR code", "width": 300 },
"background": { "url": "background.html" },
"ribbon": [{ "id": "open", "label": "QR code", "opens": "panel" }],
"contextMenu": [{ "id": "from-selection", "label": "QR code of the selection", "when": "selection" }],
"inputHelper": false,
"translations": { "de": { "QR code": "QR-Code" } }
}| 欄位 | 意義 |
|---|---|
manifestVersion, id, version | 固定為 1;唯一的反向網域名稱 id;語意化版本號。 |
name, description, author, icon | 「外掛程式」面板顯示的內容,圖示取自外掛程式的資料夾。 |
editors | 提供此外掛程式的編輯器:docx、xlsx、pptx、pdf。 |
permissions | read(讀取文件)與 write(可以變更文件)。沒有 write 的外掛程式呼叫任何寫入方法都會被拒絕。 |
connect | 外掛程式頁面可以存取的 https 來源(它自己的資料夾一律允許)。其他位址一概無法存取。 |
panel, background | 它的面板(顯示在側邊欄中)與背景頁(從一開始就執行,不可見);外掛程式至少要有其中之一。 |
ribbon, contextMenu, inputHelper | 它的功能區按鈕、右鍵選單項目(when: selection 表示只在選取文字時顯示),以及是否提供輸入輔助建議。 |
translations | 它在其他語言中的標籤,依地區設定組織;由 plugin.tr 讀取。 |
外掛程式執行環境
編輯器會在每個外掛程式頁面的最前面放入 MR Office 的外掛程式執行環境,也就是 window.MROffice.plugin,並透過一條專用通道把它連接到編輯器。
<!doctype html>
<html>
<body>
<button id="upper">UPPER CASE</button>
<script>
const plugin = MROffice.plugin;
plugin.ready.then((info) => {
document.getElementById("upper").onclick = async () => {
const { text } = await plugin.word.getSelection();
await plugin.word.insertText(text.toUpperCase());
plugin.ui.notify(plugin.tr("Done"));
};
plugin.onCommand("open", (event) => console.log(event.source, info.kind));
plugin.on("selectionChange", (event) => console.log(event.text));
});
</script>
</body>
</html>| API | 作用 |
|---|---|
plugin.ready | 編輯器連接後兌現:外掛程式的 id 與版本、檔案類型、工作階段模式、地區設定、佈景主題、它的權限,以及你的嵌入設定傳給它的 options。 |
plugin.word, .excel, .powerpoint, .pdf, .document, .getCapabilities() | 完整的物件模型,與宿主頁面取得的相同,並依外掛程式的權限與工作階段檢查。 |
plugin.on(event, listener) | 監聽 selectionChange、contentChange 與 documentReady。 |
plugin.onCommand(id, listener) | 使用者選擇外掛程式的某個按鈕或選單項目時執行程式碼。 |
plugin.ui.openPanel(), closePanel(), notify(text) | 顯示或關閉它的面板,或在編輯器中顯示一則簡短通知。 |
plugin.onInput(listener), plugin.ui.setInputHelper(items) | 在使用者輸入時於游標處提供建議(onInput 提供游標前的字詞);使用者的選擇透過 onCommand 送達。 |
plugin.tr(text) | 取自資訊清單 translations、使用編輯器語言的標籤。 |
沙箱與安全性
- 每個外掛程式頁面都在來源不透明的沙箱框架中執行:它讀不到編輯器的 Cookie、儲存空間或工作階段權杖,也讀不到編輯器所在的頁面。
- 它的內容安全性原則(Content-Security-Policy)由資訊清單產生:指令碼、樣式、圖片與字型只能來自它自己的資料夾,網路只能存取它的資料夾與它宣告的來源,不能有其他框架、表單或頁面導覽。
- 它所做的一切都經過一條通往自動化 API 的訊息通道,受工作階段權限與它自身權限的約束。唯讀工作階段不給它任何寫入的能力。
- 編輯器使用的是工作區安裝時的那份資訊清單:外掛程式無法靠日後變更它提供的檔案來擴大自己的權限。
隨嵌入一起提供外掛程式
在 open() 的 plugins 選項中列出外掛程式。options 以 (await plugin.ready).options 的形式送達外掛程式;connect 授予它更多來源,例如你自己的端點。ui.features.plugins: false 會關閉外掛程式。
MROffice.open({
container: "#editor",
token,
plugins: [
"https://cdn.example.com/plugins/qr-code/mroffice-plugin.json",
{
url: "https://mroffice.ai/plugins/samples/translate/mroffice-plugin.json",
options: { endpoint: "https://api.example.com/translate", target: "de" },
connect: ["https://api.example.com"],
},
],
ui: { features: { plugins: true, macros: true } },
});工作區外掛程式
在 MR Office 本身中,使用者在「設定 > 外掛程式」為自己的工作區安裝外掛程式:可以是範例外掛程式,也可以依資訊清單 URL 安裝任何外掛程式。安裝前,頁面會列出每個外掛程式能做什麼;安裝後,工作區的每個編輯器都會提供這些外掛程式。
ONLYOFFICE 外掛程式
- 為 ONLYOFFICE 撰寫的外掛程式(它的 config.json、頁面與 window.Asc.plugin)可以原樣在 MR Office 中執行:凡是能指定 mroffice-plugin.json 的地方,都可以改為指定它的 config.json。
- MR Office 以自己的物件模型為它提供 window.Asc.plugin:init、button、onTranslate 與 tr、onThemeChanged、executeMethod、帶 Asc.scope 的 callCommand、attachEditorEvent,以及右鍵選單項目與工具列項目。一次 callCommand 發出的 Document Builder 呼叫作為一個復原步驟執行。
- 它的 sdkjs-plugins 指令碼由 MR Office 自己提供,而且它和任何外掛程式一樣在同一個沙箱中執行:只能透過你以 connect 授予的來源存取網路。
範例外掛程式
| 資訊清單 | 外掛程式 |
|---|---|
https://mroffice.ai/plugins/samples/qr-code/mroffice-plugin.json | QR 碼:為連結或選取內容產生 QR 碼,在 Word 與 PowerPoint 中以圖片插入。 |
https://mroffice.ai/plugins/samples/translate/mroffice-plugin.json | 翻譯選取內容:把選取內容傳送到你設定的翻譯端點,譯文放回原處或以註解新增。 |
https://mroffice.ai/plugins/samples/word-count/mroffice-plugin.json | 字數與可讀性:字數、字元數、句數、Flesch 易讀度與年級水準,隨使用者輸入即時更新。 |