unsafeWindow
क्रोम, टैम्पर्मोनकी या फ़ायरफ़ॉक्स पर फ़्रेम/आइफ्रेम के साथ अच्छा नहीं खेलता है।
- jQuery (फ्रेम में) जेएस के साथ जेएस तक पहुंचने का प्रयास कर रहा है, जैसे, काम नहीं करेगा।
- उपयोगकर्तालेख
@include
, @exclude
, और/या @match
आवश्यकताओं को पूरा करते हुए iframes पर चलेंगे।
तो, आपको कई स्क्रिप्ट रनों के लिए खाते की आवश्यकता है और फिर आपके पास पूरा करने की कोशिश कर रहे हैं, इसके आधार पर आपके पास दो बुनियादी दृष्टिकोण हैं। आप कर सकते हैं:
(ए) this answer में विशिष्ट फ्रेम (ओं) के लिए स्क्रिप्ट को दर्पण करें।
या (बी) अपने जेएस इंजेक्ट करें और विशेष कार्य को पकड़ने के लिए विशेष frames
ऑब्जेक्ट का उपयोग करें।
निम्न स्क्रिप्ट दोनों को दर्शाती है। इसे Tampermonkey (या फ़ायरफ़ॉक्स Greasemonkey) में स्थापित करें, और उसके बाद 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");
आपको लगता है कि स्क्रिप्ट दोनों मुख्य पृष्ठ से और iframe से कोई ऐसा फ़ंक्शन चलाता देखेंगे। कंसोल आउटपुट (Tampermonkey) हो जाएगा:
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
यह भी एक सीधे-अप क्रोम userscript के रूप में कार्य करेगा जब आप unsafeWindow
लाइन (रों) को हटा दें।
स्रोत
2012-07-25 08:31:51
यह वही मूल है – Skinner927