{"id":387,"date":"2009-05-03T02:14:17","date_gmt":"2009-05-03T07:14:17","guid":{"rendered":"http:\/\/mikeconley.ca\/blog\/?p=387"},"modified":"2023-12-20T16:25:20","modified_gmt":"2023-12-20T21:25:20","slug":"overriding-firefoxs-windowalert-part-3","status":"publish","type":"post","link":"https:\/\/mikeconley.ca\/blog\/2009\/05\/03\/overriding-firefoxs-windowalert-part-3\/","title":{"rendered":"Overriding Firefox\u2019s Window.Alert &#8211; Part 3"},"content":{"rendered":"<p>Wow.\u00a0 I think I got it.\u00a0 I&#8217;ve got a Firefox plugin that can suppress all alert() dialogs on a page if the user checks a &#8220;suppress&#8221; box on the second alert() dialog.<\/p>\n<p>The trick, was not to rely on the DOMContentLoaded event to fire to do the override.\u00a0 Instead, I used the DOMWillOpenModalDialog to detect the first alert().\u00a0 After detection, I overrode with an alertCheck which asked the user whether or not to &#8220;suppress more dialogs&#8221;.\u00a0 If the user answers in the affirmative, alert() is simply overwritten with an empty function.<\/p>\n<p>Piece of cake.<\/p>\n<p>A couple of issues though&#8230;<\/p>\n<h2>Security<\/h2>\n<p>In order to override the alert() function, I have to write to document.getElementById(&#8216;content&#8217;).contentWindow.wrappedJSObject.alert.<\/p>\n<p>Remember how I mentioned the <em>distance<\/em> between the Extension JavaScript, and the inline content JavaScript?\u00a0 I said it felt like a security layer.<\/p>\n<p>I was totally right.<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/En\/Working_with_windows_in_chrome_code#Accessing_content_documents\">Check this out.<\/a> I&#8217;ll quote:<\/p>\n<blockquote><p>You should be aware of <a rel=\"internal\" href=\"https:\/\/developer.mozilla.org\/en\/XPCNativeWrapper\">XPCNativeWrappers<\/a> when working with <a rel=\"internal\" href=\"https:\/\/developer.mozilla.org\/en\/XPCNativeWrapper#What_is_a_trusted_window.3F\">untrusted content<\/a>. With XPCNativeWrappers turned on (which is the default in Firefox 1.5+), your extension can safely access the DOM of the content document, but not the content JavaScript. Bypassing XPCNativeWrapper to work with content JavaScript directly can lead to security problems.<\/p><\/blockquote>\n<p>Hrmph.\u00a0 So I seem to be violating some security rules here.\u00a0 So maybe my approach isn&#8217;t the greatest idea.\u00a0 &#8220;Mook&#8221; from irc.mozilla.net #extdev suggested looking into commonDialog.xul&#8230;but I can&#8217;t seem to wrap my head around that just yet.<\/p>\n<h2>Imperfections<\/h2>\n<p>Not sure why yet, but while I can suppress dialog floods like this:<\/p>\n<pre>for (i = 0; i &lt; 10; ++i) {\r\n  alert(i);\r\n}<\/pre>\n<p>It seems to fail on this:<\/p>\n<pre>for (i = 0; i &lt; 10; ++i) {\r\n  alert(i);\r\n  confirm(i);\r\n}<\/pre>\n<p>For some reason, regardless of whether or not I choose to suppress the dialogs, they just keep coming.\u00a0 It works fine when I swap out the confirm() for a second alert().\u00a0 Not exactly sure why.\u00a0 Yet.<\/p>\n<p>Ok, so I&#8217;m going to clean the code up, and post it soon.\u00a0 I&#8217;ll also post a link to a real, brutally annoying website where you can test the alertCheck extension.\u00a0 Just give me a bit.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wow.\u00a0 I think I got it.\u00a0 I&#8217;ve got a Firefox plugin that can suppress all alert() dialogs on a page if the user checks a &#8220;suppress&#8221; box on the second alert() dialog. The trick, was not to rely on the DOMContentLoaded event to fire to do the override.\u00a0 Instead, I used the DOMWillOpenModalDialog to detect [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[380,5,220,44,68,51,79],"tags":[207,210,214,219,216,217,208,125,1212,35,209,212,218],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-alertcheck-firefox-extensions-technology","category-computer-science","category-firefox-extensions","category-internet","category-javascript","category-security","category-technology","tag-alert","tag-alertcheck","tag-contentalert","tag-contentwindow","tag-domcontentloaded","tag-domwillopenmodaldialog","tag-dos","tag-firefox","tag-javascript","tag-mozilla","tag-override","tag-windowalert","tag-wrappedjsobject"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/prmTy-6f","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":5,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/posts\/387\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mikeconley.ca\/blog\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}