Advertisement
KiberInfinity

ExtensionMsgBridge

Apr 30th, 2013
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ExtensionMsgBridge = {
  2.    callbacks: ["null"],
  3.    listen_event:'ext_1',
  4.    send_event:'ext_2',
  5.    send_request: function (data, callback) { // analogue of chrome.extension.sendRequest  
  6.       var set_data = function (el, field, data) {
  7.          if(el.setUserData) {
  8.             el.setUserData(field, data, null);
  9.          } else {
  10.             el.dataset[field] = JSON.stringify(data);
  11.          }
  12.       }
  13.       var get_data = function (el, field) {
  14.          if(el.getUserData) {
  15.             return el.getUserData(field);
  16.          } else {
  17.             return JSON.parse(el.dataset[field]);
  18.          }
  19.       }
  20.       var request = null;
  21.       request = document.createElement("div");
  22.       set_data(request, "data", data);
  23.       if(callback) {
  24.          var callback_idx = ExtensionMsgBridge.callbacks.length;
  25.          ExtensionMsgBridge.callbacks.push(function (response) {
  26.             ExtensionMsgBridge.callbacks[callback_idx] = 'done';
  27.             //alert('Before callback');
  28.             return callback(response);
  29.          });
  30.          set_data(request, "callback", callback_idx);
  31.          document.addEventListener(ExtensionMsgBridge.send_event+"-response", function (event) {
  32.             var node = event.target,
  33.                callback = ExtensionMsgBridge.callbacks[get_data(node, "callback")],
  34.                response = get_data(node, "response");
  35.             if (node.parentNode) node.parentNode.removeChild(node);
  36.             document.removeEventListener(ExtensionMsgBridge.send_event+"-response", arguments.callee, false);
  37.             return callback(response);
  38.          }, false);
  39.       }
  40.       document.documentElement.appendChild(request);
  41.       var sender = document.createEvent("HTMLEvents");
  42.       sender.initEvent(ExtensionMsgBridge.send_event+"-query", true, false);
  43.       return request.dispatchEvent(sender);
  44.    },
  45.    listen_request: function (callback) {
  46.       var set_data = function (el, field, data) {
  47.          if(el.setUserData) {
  48.             el.setUserData(field, data, null);
  49.          } else {
  50.             el.dataset[field] = JSON.stringify(data);
  51.          }
  52.       }
  53.       var get_data = function (el, field) {
  54.          if(el.getUserData) {
  55.             return el.getUserData(field);
  56.          } else {
  57.             return JSON.parse(el.dataset[field]);
  58.          }
  59.       }
  60.       return document.addEventListener(ExtensionMsgBridge.listen_event+"-query", function (event) {
  61.          var node = event.target,
  62.             doc = node.ownerDocument;
  63.          return callback(get_data(node, "data"), doc, function (data) {
  64.             if(!get_data(node, "callback")) {
  65.                if (node.parentNode) node.parentNode.removeChild(node);
  66.                return
  67.             }
  68.             set_data(node, "response", data)
  69.             var listener = doc.createEvent("HTMLEvents");
  70.             listener.initEvent(ExtensionMsgBridge.listen_event+"-response", true, false);
  71.             return node.dispatchEvent(listener);
  72.          });
  73.       }, false, true);
  74.    },
  75.    listen_handler: function (request, sender, callback) {
  76.       console.log('on message '+ExtensionMsgBridge.listen_event, request,sender);
  77.       callback({
  78.          response: 'params:'+JSON.stringify(request)
  79.       });      
  80.    }
  81. }
  82.  
  83.  
  84.  
  85. // Set event id for extension content script side
  86. ExtensionMsgBridge.listen_event='ext_side2';
  87. ExtensionMsgBridge.send_event='ext_side1';
  88. ExtensionMsgBridge.listen_request(ExtensionMsgBridge.listen_handler);  
  89.  
  90.  
  91. /*
  92. // Set event id for injected script side
  93. ExtensionMsgBridge.listen_event='ext_side1';
  94. ExtensionMsgBridge.send_event='ext_side2';
  95. ExtensionMsgBridge.listen_request(ExtensionMsgBridge.listen_handler);  
  96. */
Advertisement
RAW Paste Data Copied
Advertisement