mikitex70 / redmine_drawio

Macro plugin to embed draw.io diagrams into Redmine wiki pages
MIT License
121 stars 51 forks source link

Failed to execute 'addEventListener' on 'EventTarget': parameter 2 is not of type 'Object'. #105

Closed koboldMaki closed 2 years ago

koboldMaki commented 2 years ago

Following error occurs when attaching an XML or drawio drawing on any redmine page:

Failed to execute 'addEventListener' on 'EventTarget': parameter 2 is not of type 'Object'.

Environment: Redmine version 4.2.1.stable Ruby version 2.7.0-p0 (2019-12-25) [x86_64-linux-gnu] Rails version 5.2.5 Environment production Database adapter Mysql2 Mailer queue ActiveJob::QueueAdapters::AsyncAdapter Mailer delivery smtp SCM: Subversion 1.13.0 Git 2.25.1 Filesystem
Redmine plugins: menu_new_issue 0.0.1 redmine_drawio 1.2.0 redmine_latex_mathjax 0.4.0 redmine_wysiwyg_editor 0.23.0 redmine_zulip 2.1.2

Error occurs also at redmine_drawio 1.1.4 i used before patch to 1.2.0 today but before holidays all works fine.

mikitex70 commented 2 years ago

Hi @koboldMaki, thanks for reporting the issue. The error was caused by a change in an external javascript from embed.diagrams.net (drawio site) which is included and patched runtime, used to render diagrams in XML format. Sorry for this, but I don't have control on those files...

gavin887 commented 2 years ago

just add this code:

    var searchRegex = /(mxEvent\.addListener\([^,]+,[^,]+),([ a-zA-Z0-9]+)(\))/g;
    var replaceRules = "$1,(typeof($2)=='string')?eval($2):$2$3"
    code = code.replaceAll(searchRegex, replaceRules);

above assets/javascripts/drawioEditor.js:568

    // Apply the patch
    GraphViewer.prototype.addToolbar = eval("("+code+")");

thanks for you great works, @mikitex70

koboldMaki commented 2 years ago

Works great, big thanks!

mikitex70 commented 2 years ago

Hi @gavin887, your fix works fine but if think it may touch too many places. With your code I realized that the only important code to fix is:

mxEvent.addListener(g,"click",b);

which is near the beginning of the script, easier to find and less likely to be interrupted by new lines (which was the cause of this issue). So I came to this patch code:

// Patch the code
var code = GraphViewer.prototype.addToolbar.toString();
var searchRegex = /mxEvent\.addListener\(([a-zA-Z]),"click",([a-zA-Z])\)/;
var replaceRules = "mxEvent.addListener($1,\"click\",(typeof($2)==='string'?eval($2):$2))";

code = code.replace(searchRegex, replaceRules);
// Apply the patch
GraphViewer.prototype.addToolbar = eval("("+code+")");

It wiill be included in the next release. Thanks again for your suggestion, which suggested a simpler and more stable code.

gavin887 commented 2 years ago

nice

s-rakowski commented 2 years ago

Hello. I've noticed that a recent diagrams.net update caused this error to return. There is a following line in the minified JS now:

mxEvent.addListener(I, "click", ea);

Variable name "ea" is not being caught by the regular expression in drawioEditor.js, as it only matches single letter names. I suggest changing it to:

var searchRegex = /mxEvent\.addListener\(([a-zA-Z]+),"click",([a-zA-Z]+)\)/;
mikitex70 commented 2 years ago

Solved, thanks for reporting the issue. Please, next time open a new report, it is easier to track changes in the CHANGELOG :smiley:.