zotero/zotero — pull request #218 by aurimasv (2012-12-22 23:51:59+00:00)
Title: [Do Not Merge] Allow translators to re-trigger detectWeb on DOM changes

This is in regards to websites that update their pages without triggering a pageLoad event. Since monitoring all DOM changes on all websites would be too much, I figured that each translator should be able to tell Zotero to monitor certain DOM nodes for changes and redo detectWeb.

E.g. the ARTstor translator is currently more or less broken because of heavy use of JavaScript on their website. Using this patch, you could do something like:
```
function detectWeb(doc, url) {
if(doc.body.id == "canvas") {
Zotero.monitorDOMChanges(doc.body);
}

...
}
```

At least that's how it should work in theory.

I am currently running into one problem that I cannot wrap my head around: `MutationObserver` is undefined inside `monitorDOMChanges`.

It is running inside a sandbox, so it kind of makes sense (though I'm still not sure how to figure out if an object will be defined inside a sandbox or not.... E.g. `Math` is defined, so why not `MutationObserver`??). I was looking at some code used to expose `DOMParser` to translators and I think I could use the same method, but the thing is that I don't want/need `MutationObserver` to be accessible by the translators themselves, just the `monitorDOMChanges` function. Should I be defining `monitorDOMChanges` as `Zotero.Translate.Web.prototype.monitorDOMChanges` instead of inside `Zotero.Translate.Sandbox`? But I don't think that's an appropriate place for it in terms of the conditions that the function is used under (i.e. it is only ever called from translators)

So what are my options here? (Also, is this an OK approach to the bigger problem?)