unsafeWindow
no funciona bien con marcos/iframes en Chrome, Tampermonkey o Firefox.
- Intentar acceder a JS global (al marco) con jQuery, de esa manera, no funcionará.
- los usuarios se ejecutarán en iframes que cumplan los requisitos
@include
, @exclude
y/o @match
.
Por lo tanto, debe tener en cuenta las secuencias de comandos múltiples y, a continuación, tiene dos enfoques básicos, en función de lo que está tratando de lograr. Puede:
(A) Adapte la secuencia de comandos a marcos específicos, como en this answer.
o (B) inyecte su JS y utilice el objeto especial frames
para obtener la función específica que desee.
El siguiente script muestra ambos. Instálelo en Tampermonkey (o Firefox Greasemonkey), y luego visite this test page at jsBin.
// ==UserScript==
// @name _Calling iframe functions
// @namespace _pc
// @include http://jsbin.com/ugoruz/*
// @include http://jsbin.com/okequw/*
// ==/UserScript==
console.log ("Script start...");
/*--- This next function call will work in Firefox or Tampermonkey ONLY,
not pure Chrome userscript.
*/
console.log ("calling functionOfInterest()...");
unsafeWindow.functionOfInterest();
if (window.top === window.self) {
//--- Code to run when page is the main site...
console.log ("Userscript is in the MAIN page.");
//--- The frames object does not play nice with unsafeWindow.
/*--- These next three work in Firefox, but not Tampermonkey, nor pure Chrome.
console.log ("1", frames[1].variableOfInterest); // undefined
console.log ("2", unsafeWindow.frames[1].variableOfInterest); // undefined
console.log ("3", frames[1].unsafeWindow); // undefined
*/
/*--- This next would cause a silent crash, all browsers...
console.log ("4", unsafeWindow.frames[1].unsafeWindow.variableOfInterest);
*/
//--- To get at iFramed JS, we must inject our JS.
withPages_jQuery (demoAccessToFramedJS);
}
else {
//--- Code to run when page is in an iframe...
console.log ("Userscript is in the FRAMED page.");
console.log ("The frame's ID is:", window.self.frameElement.id);
}
function demoAccessToFramedJS ($) {
$("body").prepend (
'<button id="gmMain">Run JS on main window</button>'
+ '<button id="gmFrame">Run JS on iframe</button>'
);
$("#gmMain, #gmFrame").click (function() {
if (this.id === "gmMain") {
functionOfInterest();
}
else {
frames[1].functionOfInterest();
}
console.log (this.id + "was clicked.");
});
}
function withPages_jQuery (NAMED_FunctionToRun) {
//--- Use named functions for clarity and debugging...
var funcText = NAMED_FunctionToRun.toString();
var funcName = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
var script = document.createElement ("script");
script.textContent = funcText + "\n\n";
script.textContent += 'jQuery(document).ready(function() {'+funcName+'(jQuery);});';
document.body.appendChild (script);
};
console.log ("Script end");
Usted verá que el script se ejecuta una función tanto de la página principal y desde el marco flotante. La salida de la consola (Tampermonkey) será:
Tampermonkey started
Script start...
calling functionOfInterest()...
Userscript is in the MAIN page.
Script end
Tampermonkey started
Script start...
calling functionOfInterest()...
Userscript is in the FRAMED page.
The frame's ID is: iframe2
Script end
También funcionará como un userscript cromo recto-para arriba si se quita la línea (s) unsafeWindow
.
Todo es el mismo origen – Skinner927