From 1b0b32e2f13843ed0189e1a7856d99218d683306 Mon Sep 17 00:00:00 2001 From: david g Date: Thu, 25 May 2017 09:59:18 -0400 Subject: [PATCH] #40 - Avoid cluttering the console on AEM Plugin errors when its invoked on non-candidate pages --- ...e-plugin--adaptive-form--content-script.js | 24 +++++++++++-------- ...me-plugin--adaptive-form--custom-script.js | 16 ++++++++----- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/aem-chrome-plugin--adaptive-form--content-script.js b/aem-chrome-plugin--adaptive-form--content-script.js index cc1e06b..a76aecb 100644 --- a/aem-chrome-plugin--adaptive-form--content-script.js +++ b/aem-chrome-plugin--adaptive-form--content-script.js @@ -18,15 +18,19 @@ * #L% */ -(function(){ - var s = document.createElement('script'); - s.src = chrome.extension.getURL('aem-chrome-plugin--adaptive-form--custom-script.js'); - (document.head || document.documentElement).appendChild(s); +try { + (function(){ + var s = document.createElement('script'); + s.src = chrome.extension.getURL('aem-chrome-plugin--adaptive-form--custom-script.js'); + (document.head || document.documentElement).appendChild(s); - window.addEventListener('af-editor-loaded.afPlugin', function () { - chrome.runtime.sendMessage({url: window.location.href, action: "af-editor-loaded"}, function (response) { - // Reduce console.log clutter - // console.log(response); + window.addEventListener('af-editor-loaded.afPlugin', function () { + chrome.runtime.sendMessage({url: window.location.href, action: "af-editor-loaded"}, function (response) { + // Reduce console.log clutter + // console.log(response); + }); }); - }); -})(); + })(); +} catch(err) { + // Do not bother user if an error occurs, as this is loaded everywhere. +} \ No newline at end of file diff --git a/aem-chrome-plugin--adaptive-form--custom-script.js b/aem-chrome-plugin--adaptive-form--custom-script.js index 150ab17..48f6c6b 100644 --- a/aem-chrome-plugin--adaptive-form--custom-script.js +++ b/aem-chrome-plugin--adaptive-form--custom-script.js @@ -34,12 +34,16 @@ document.addEventListener("DOMContentLoaded", function () { * So, we set a flag (editorFrameLoaded) on window. In authorUtils, we evaluate this flag * on the console and display the authoring frame. */ - if(window.jQuery) { - $(document).on("cq-editor-loaded", function () { - window.editorFrameLoaded = true; - window.dispatchEvent(new Event('af-editor-loaded.afPlugin')); - addAuthoringUtils(); - }); + try { + if ($) { + $(document).on("cq-editor-loaded", function () { + window.editorFrameLoaded = true; + window.dispatchEvent(new Event('af-editor-loaded.afPlugin')); + addAuthoringUtils(); + }); + } + } catch(err) { + // Do not bother user with errors as this script can be injected in unexpected contexts (though we try not to). } });