• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sending a message from a CI application to a content script

#1
I have a CI application and I also have a Chrome Extension. Sometimes the CI app is run by a user and they don't have the extension installed. Sometimes the Extension is installed and the user is not running the CI app. I now want to allow the Extension to know if the CI app is running on the same PC. 

I ultimately want to pass messages from the CI app to the Extension running in the same copy of Chrome.  I understand that there is a thing called a content script that can be run from the tab where the CI app is run. I can spawn a content_script.js whenever the CI app is running. But my communications between the two is really stupid. The content script can communicate to my Extension's Background script without too much problem. And the background script (part of the extension) can talk to both the popup script and to the server via Ajax. 

The weak point is communicating from the tab running the CI app to the content script. This is because all the examples of this communication are in javascript and I really don't know how to run a javascript command in the CI application. What I'd like to do is to  allow the user to press a button in GroceryCrud (used pretty exstensivly) and send some data in a message from the CI app to the content script. 

Can anyone point me to a document or an example of how to get a CI app to send a message to a content script, or at least how to run a javascript line in CI?
Reply

#2
CI doesn't "talk" to client-side scripts, but it can respond to AJAX requests from them.
There could be an addin that integrated the two more along the lines of what you are looking for.
Reply

#3
(10-18-2018, 06:21 PM)ciadmin Wrote: CI doesn't "talk" to client-side scripts, but it can respond to AJAX requests from them.
There could be an addin that integrated the two more along the lines of what you are looking for.
I already have a background script that "talks" to a CI daemon. But I am looking to go the other way, from the CI app (in the browser) to the background script. The background script could "poll" the CI app to see if the user logged on to the CI app. But I really think this is wasteful since 99% of the Extension users won't be running the the CI app. I think I need to go from the CI display (perhaps by injecting a frame?) in the browser to the content script.
Reply

#4
You are correct - a background script can "talk" to a CI daemon.
I don't know how to go the other way ... web sockets, perhaps? I have seen some awkward impolementations that try to integrate that with PHP.
Reply

#5
Don't know if this helps. I use mqtt messaging on our system. When an order lands in the db, the server (php) broadcasts a message which is picked up by browsers (javascript) subscribed to the appropriate topic. This way, users see immediately when the list of orders or the status of an order changes, no need for them to click a refresh button or have a background timer polling the server.
Reply

#6
(10-18-2018, 10:23 PM)ciadmin Wrote: You are correct - a background script can "talk" to a CI daemon.
I don't know how to go the other way ... web sockets, perhaps? I have seen some awkward impolementations that try to integrate that with PHP.

Well, I have found some examples of an HTML page with js code "injected" sending messages to a content script. Someone talked about doing this in an iframe (whatever that is). BTW, I really don't need the content script to "send messages back" to the web page. This is because the content script talks with the background script and the background script talks to both the Extension (it is actually part of the extension) AND the daemon on the server (via AJAX).  

I just don't know how to create an iframe in a CI generated HTML page??  Any idea how to do that?

I have managed to get the icon for the extension to appear differently whenever my CI app is up and running in one of the browser tabs. But when the tab with the CI app closes, I'd like it to go back to the original. Right now it won't go back to the original until I reload the Extension! Not optimal.

I found this code to send a message from an html page to a be "caught" by a content script.
Code:
       <script>
           var go = function() {
               var event = document.createEvent('Event');
               event.initEvent('hello');
               document.dispatchEvent(event);
           }
       </script>
       <a href="javascript:go();">Click me</a>
I understand how it is supposed to work except the last line, javascript:go()?? I guess that runs the go function? Can I get this function go to run by itself without a click?
Reply

#7
OK. By using the above logic I was able to get messages from the browser tab out to my content script. One small problem! Any ideas?

This is the line that gets executed when a user wants to logoff.

<li><a href="javascript:LogoffSu();"<?php echo site_url('Users/logout')?>'>Logout</a></li>

I modified it so that I run the javascript LogoffSu()  first and that sends the event. But now, Users/logout is not running. Here is the LogoffSu()

          var LogoffSu = function() {
               var event = document.createEvent('Event');
               event.initEvent('LogoffSu');
               document.dispatchEvent(event);
               return;
           }

Also, if the user closes the tab, I want to run that same LogoffSu(). Is that possible? I added this but it doesn't seem to work:
<body onload="LoginSu();" onunload="LogoffSu();">

Rich
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.