0

我正在构建一个与 draw.io 交互的 Chrome 扩展。Draw.io 创建了一个 EditorUI 实例,该实例包含有关当前打开的图表的所有信息(包括图表的 SVG 图像)。是否可以使用 JavaScript 访问该上下文?窗口变量(可通过代码插入访问)仅包含创建 EditorUI 实例的函数,但不包含实例本身。我无法获得州/地方范围。

4

3 回答 3

1

App.main 有一个返回 ui 实例的回调。

于 2017-03-15T09:17:35.833 回答
1

这是我解决它的方法:注入函数是正确的方法。起初我希望简单地调用 Draw.io 函数getCurrentFile();来接收所需的信息。虽然可以打电话给他们,但你什么也得不到null。因此,我决定重写该函数,保留原始内容并将我需要的内容发送到自定义事件侦听器。

var svg = null;

/**
 * Function to be inserted into Draw.io. Contains a function that overrides getCurrentFile(). Original functionality
 * of getter function is kept, file data is saved to a variable.
 */
var hijackFileGetter = '(' + function() {
        window.EditorUi.prototype.getCurrentFile = function() {
            if(this.currentFile !== null) {
                var svg = this.currentFile.data;
                var event = document.createEvent("CustomEvent");
                event.initCustomEvent("listenToChanges", true, true, svg);
                document.dispatchEvent(event);
            }
            return this.currentFile;
        };
    } + ')();';

/**
 * Injection of Javascript code into Draw.io
 */
var script = document.createElement('script');
script.textContent = hijackFileGetter;
(document.head||document.documentElement).appendChild(script);
script.remove();

/**
 * EventListener for communication from our injected script to this content script
 * Receives a new SVG every time changes are made
 */
document.addEventListener('listenToChanges', function (data) {
    svg = data.detail;
});

/**
 * Message passing from content script to popup (and the other way around)
 */
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if(request.popup) {
        chrome.runtime.sendMessage({svg: svg});
        sendResponse({svgSearch: true});
    }
});
于 2017-03-16T12:02:22.553 回答
0

回调对我有帮助!

我从 GraphEditor 移到 draw.io 并且无法获取 UI 对象

            `var MyUi;
            App.main(function(ui) {
            MyUi = ui;
            var fmi2 = new FMI2(ui);
            fmi2.init();
            //SidebarInit.apply(Edo.sidebar, arguments);
            //About Dialog
            fmi2.OverrideAbout();
            //Graph behaviour
            fmi2.fmiGraphSettings();
            //fmiGraphSettings(Edo.editor.graph);
            //Pod & Booth Controls
            fmi2.AddFMIControls();
            //AddPodControl(Edo);
            //Hints
            AddHints();
          });`
于 2020-01-18T02:18:13.833 回答