Skip to content

Commit cb139e8

Browse files
committed
Fixed adam-p#435: console errors on cross-origin iframes-with-iframes
This is related to adam-p#173, but the fix for that didn't check deeply enough.
1 parent e254b5f commit cb139e8

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

Diff for: src/common/CHANGES.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Change Log
22
==========
33

4+
2017-xx-yy: v2.13.3
5+
--------------------
6+
7+
* [Fixed bug #435](https://github.com/adam-p/markdown-here/issues/435): On some pages, Markdown Here would spew cross-origin exceptions to the console. This was due to MDH trying to determine if a focused iframe-within-an-iframe was renderable.
8+
- Thanks to [lincoln-b](https://github.com/lincoln-b) for reporting it.
9+
10+
411
2017-05-26: v2.13.1
512
--------------------
613

Diff for: src/common/markdown-here.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,39 @@ var mylog = function() {};
6060
function findFocusedElem(document) {
6161
var focusedElem = document.activeElement;
6262

63-
// Fix #173: https://github.com/adam-p/markdown-here/issues/173
64-
// If the focus is in an iframe with a different origin, then attempting to
65-
// access focusedElem.contentDocument will fail with a `SecurityError`:
66-
// "Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://jsbin.io" from accessing a cross-origin frame."
67-
// Rather than spam the console with exceptions, we'll treat this as an
68-
// unrenderable situation (which it is).
69-
try {
70-
var accessTest = focusedElem.contentDocument;
63+
// Tests if it's possible to access the iframe contentDocument without throwing
64+
// an exception.
65+
function iframeAccessOkay(focusedElem) {
66+
// Fix #173: https://github.com/adam-p/markdown-here/issues/173
67+
// Fix #435: https://github.com/adam-p/markdown-here/issues/435
68+
// If the focus is in an iframe with a different origin, then attempting to
69+
// access focusedElem.contentDocument will fail with a `SecurityError`:
70+
// "Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://jsbin.io" from accessing a cross-origin frame."
71+
// Rather than spam the console with exceptions, we'll treat this as an
72+
// unrenderable situation (which it is).
73+
try {
74+
var _ = focusedElem.contentDocument;
75+
}
76+
catch (e) {
77+
// TODO: Check that this is actually a SecurityError and re-throw if it's not?
78+
return false;
79+
}
80+
81+
return true;
7182
}
72-
catch (e) {
73-
// TODO: Check that this is actually a SecurityError and re-throw if it's not?
83+
84+
if (!iframeAccessOkay(focusedElem)) {
7485
return null;
7586
}
7687

7788
// If the focus is within an iframe, we'll have to drill down to get to the
7889
// actual element.
7990
while (focusedElem && focusedElem.contentDocument) {
8091
focusedElem = focusedElem.contentDocument.activeElement;
92+
93+
if (!iframeAccessOkay(focusedElem)) {
94+
return null;
95+
}
8196
}
8297

8398
// There's a bug in Firefox/Thunderbird that we need to work around. For

0 commit comments

Comments
 (0)