From 9715335f0b05a9de87c8781c54d14fda0c671b29 Mon Sep 17 00:00:00 2001
From: Nikita Petrov <nikita.petrov.drupal@gmail.com>
Date: Sun, 26 Jan 2014 18:13:18 +0400
Subject: [PATCH] Add advertisement to Javascript console

---
 js/consolelog.min.js       |  8 ++++++++
 js/source_ad_js_console.js | 13 +++++++++++++
 source_ad.module           | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 js/consolelog.min.js
 create mode 100644 js/source_ad_js_console.js

diff --git a/js/consolelog.min.js b/js/consolelog.min.js
new file mode 100644
index 0000000..1a0405e
--- /dev/null
+++ b/js/consolelog.min.js
@@ -0,0 +1,8 @@
+/**
+ * Cross-Browser console.log() Wrapper
+ *
+ * Version 2.0.0, 2013-10-20
+ * By Craig Patik
+ * https://github.com/patik/console.log-wrapper/
+ *//*global log:true */// Tell IE9 to use its built-in console
+Function.prototype.bind&&/^object$|^function$/.test(typeof console)&&typeof console.log=="object"&&typeof window.addEventListener=="function"&&["log","info","warn","error","assert","dir","clear","profile","profileEnd"].forEach(function(method){console[method]=this.call(console[method],console)},Function.prototype.bind);window.log||(window.log=function(){var i,sliced,args=arguments,isIECompatibilityView=!1,isIE8=function _isIE8(){return(!Function.prototype.bind||Function.prototype.bind&&typeof window.addEventListener=="undefined")&&typeof console=="object"&&typeof console.log=="object"};log.history=log.history||[];log.history.push(arguments);log.detailPrint&&log.needsDetailPrint&&function(){var ua=navigator.userAgent,winRegexp=/Windows\sNT\s(\d+\.\d+)/;console&&console.log&&/MSIE\s(\d+)/.test(ua)&&winRegexp.test(ua)&&parseFloat(winRegexp.exec(ua)[1])>=6.1&&(isIECompatibilityView=!0)}();if(isIECompatibilityView||typeof console.log=="function"){sliced=Array.prototype.slice.call(args);if(log.detailPrint&&log.needsDetailPrint){console.log("-----------------");args=log.detailPrint(args);i=0;while(i<args.length){console.log(args[i]);i++}}else sliced.length===1&&typeof sliced[0]=="string"?console.log(sliced.toString()):console.log(sliced)}else if(isIE8())if(log.detailPrint){args=log.detailPrint(args);args.unshift("-----------------");i=0;while(i<args.length){Function.prototype.call.call(console.log,console,Array.prototype.slice.call([args[i]]));i++}}else Function.prototype.call.call(console.log,console,Array.prototype.slice.call(args));else if(!document.getElementById("firebug-lite")){(function(){var script=document.createElement("script");script.type="text/javascript";script.id="firebug-lite";script.src="https://getfirebug.com/firebug-lite.js";document.getElementsByTagName("HEAD")[0].appendChild(script)})();setTimeout(function(){window.log.apply(window,args)},2e3)}else setTimeout(function(){window.log.apply(window,args)},500)});
\ No newline at end of file
diff --git a/js/source_ad_js_console.js b/js/source_ad_js_console.js
new file mode 100644
index 0000000..35373b9
--- /dev/null
+++ b/js/source_ad_js_console.js
@@ -0,0 +1,13 @@
+/**
+  * @file
+  * Adds ad to a javascript console
+  *
+  */
+ 
+(function ($) {
+  Drupal.behaviors.source_ad_js_console = {
+    attach: function(context, settings) {
+      log(Drupal.settings.source_ad.javascript_console_message);
+    }
+  };  
+})(jQuery);
diff --git a/source_ad.module b/source_ad.module
index 40cee65..691c7cd 100644
--- a/source_ad.module
+++ b/source_ad.module
@@ -64,6 +64,21 @@ function source_ad_config_form($form, &$form_state) {
     '#description' => t('This text will be added to HTTP response headers.') . '<br/>' .
       t('Leave this field blank to remove HTTP header ad.'),
   );
+  
+  $form['source_ad_javascript_console'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Advertisement for javascript console'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+  
+  $form['source_ad_javascript_console']['javascript_console_message'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Your Ad for browser\'s javascript console'),
+    '#default_value' => filter_xss_admin(variable_get('source_ad_javascript_console_message', '')),
+    '#description' => t('This text will be published in the web browser\'s javascript console (if enabled). Please do not use HTML.') . '<br/>' .
+      t('Leave this field blank to remove your ad.'),
+  );
 
   $form['submit'] = array(
     '#type' => 'submit',
@@ -82,6 +97,7 @@ function source_ad_config_form($form, &$form_state) {
 function source_ad_config_form_submit(&$form, &$form_state) {
 
   $content = htmlentities($form_state['values']['source_ad_content'], ENT_NOQUOTES, 'UTF-8');
+  $javascript_console_message = htmlentities($form_state['values']['javascript_console_message'], ENT_NOQUOTES, 'UTF-8');
   $header_name = htmlentities($form_state['values']['header_name'], ENT_NOQUOTES, 'UTF-8');
   $header_value = $form_state['values']['header_value'];
   $options = array(SOURCE_AD_ADVERTISEMENT, SOURCE_AD_HACKER, SOURCE_AD_READ_ME);
@@ -94,6 +110,7 @@ function source_ad_config_form_submit(&$form, &$form_state) {
 
   // @todo: Convert -- to - -.
   variable_set('source_ad_content', $content);
+  variable_set('source_ad_javascript_console_message', $javascript_console_message);
 
   if ($header_value) {
     variable_set('source_ad_header_name', $header_name);
@@ -153,6 +170,7 @@ function source_ad_boot() {
     drupal_add_http_header($header_name, $header_value, FALSE);
   }
 }
+
 /**
  * Convert data to ASCII.
  *
@@ -180,3 +198,27 @@ function _source_ad_convert_to_ascii($data, $encoding) {
 
   return $out;
 }
+
+/**
+ * Implements hook_init().
+ *
+ * Add ad to javascript console.
+ * 
+ * We use "consolelog" script to show the message in console of ALL browsers. 
+ * Read more on https://github.com/patik/console.log-wrapper/blob/master/Readme.md
+ */
+function source_ad_init() {
+  $advertisement = variable_get('source_ad_javascript_console_message', '');
+  if ($advertisement) {
+    drupal_add_js(
+      array(
+        'source_ad' => array(
+          'javascript_console_message' => $advertisement,
+        ),
+      ),
+      'setting'
+    );
+    drupal_add_js(drupal_get_path('module', 'source_ad') . '/js/consolelog.min.js');
+    drupal_add_js(drupal_get_path('module', 'source_ad') . '/js/source_ad_js_console.js');
+  }
+}
-- 
1.8.1.msysgit.1

