My problem is trying to get the content of the Tumblr's tinymce editors with an userscript. In firefox this works perfectly:
var tm = (unsafeWindow.tinyMCE) ? unsafeWindow.tinyMCE : null;
if(tm!= null){
var _tempcontent = tm.activeEditor.getContent();
console.log(_tempcontent);
}
But in Chrome i get "undefined" and then when I revised the unsafeWindow i couldn't find the tinymce instance, while in Firefox exists.
Although Google Chrome now defines unsafeWindow
for userscripts, it does not allow any access to the target page's javascript objects. It only allows access to the DOM.
To workaround, you can provide the script with a cross-browser, full-featured unsafeWindow
-- as shown in this answer:
/*--- Create a proper unsafeWindow object on browsers where it doesn't exist
(Chrome, mainly).
Chrome now defines unsafeWindow, but does not give it the same access to
a page's javascript that a properly unsafe, unsafeWindow has.
This code remedies that.
*/
var bGreasemonkeyServiceDefined = false;
try {
if (typeof Components.interfaces.gmIGreasemonkeyService === "object") {
bGreasemonkeyServiceDefined = true;
}
}
catch (err) {
//Ignore.
}
if ( typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
unsafeWindow = ( function () {
var dummyElem = document.createElement('p');
dummyElem.setAttribute ('onclick', 'return window;');
return dummyElem.onclick ();
} ) ();
}
OR, you can refactor your script to use script injection...
function main () {
//--- PUT EVERYTHING IN THIS WRAPPER FUNCTION...
var tm = tinyMCE;
if (tm!= null) {
var _tempcontent = tm.activeEditor.getContent();
console.log(_tempcontent);
}
}
addJS_Node (null, null, main);
function addJS_Node (text, s_URL, funcToRun) {
var D = document;
var scriptNode = D.createElement ('script');
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
//--- Don't error check here. if DOM not available, should throw error.
targ.appendChild (scriptNode);
}