Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Neel If I understand your requirements correctly, @Rob is essentially correct though a little clarification may help</p> <p>By design/architecture, the <em>extension.js</em> code runs on each HTML page i.e. a separate <em>extension.js</em> instance is run for each URL that loads. In contrast, the context menu runs at the browser level (not HTML page) and is hence correctly coded in <em>background.js</em> file. However, the <em>background.js</em> code does not have direct access to the <em>extension.js</em> instance code running on the HTML page in the active tab and must therefore <a href="http://docs.crossrider.com/#!/api/appAPI.message">communicate the data via messaging</a>. (For more information about scopes, see <a href="http://docs.crossrider.com/#!/guide/scopes">Scopes Overview</a>)</p> <p>Obviously, a user clicks the context menu item on the active tab (i.e. the page showing the HTML page being viewed); hence, once the <strong>ContextData</strong> string is created, you can use <a href="http://docs.crossrider.com/#!/api/appAPI.message-method-toActiveTab">appAPI.message.toActiveTab</a> to send the string to the <em>extension.js</em> instance running on the page/tab where the the context menu item was clicked.</p> <p>This being the case, using your code example you can achieve this goal as follows:</p> <p><strong>background.js</strong>:</p> <pre><code>appAPI.ready(function($) { var ContextData; appAPI.contextMenu.add("key1", "Send Data To Server", function (data) { var ContextData = 'pageUrl: ' + data.pageUrl + '\r\n' + 'linkUrl: ' + data.linkUrl + '\r\n' + 'selectedText:' + data.selectedText + '\r\n' + 'srcUrl:' + data.srcUrl; appAPI.message.toActiveTab({type:'dataToSend', data: ContextData}); }, ["all"]); }); </code></pre> <p><strong>extension.js</strong>:</p> <pre><code>appAPI.ready(function($) { var dataToSend =="test data"; appAPI.message.addListener(function(msg) { if (msg.type === 'dataToSend') { appAPI.request.post({ url: 'REST API URL', postData: dataToSend, onSuccess: function(response, additionalInfo) { var details = {}; details.response = response; }, onFailure: function(httpCode) { // alert('POST:: Request failed. HTTP Code: ' + httpCode); } }); } }); }); </code></pre> <p>[<strong>Disclaimer</strong>: I am a Crossrider employee]</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload