Note that there are some explanatory texts on larger screens.

plurals
  1. POContent Script and Background Communication
    primarykey
    data
    text
    <p>I am looking to create an extension for a particular site to provide additional formatting and sharing options that they don't currently have.</p> <p>I am having issues getting things to communicate properly and there doesn't seem to be a clearly laid out example.</p> <p>Manifest:</p> <pre><code>{ "name": "Test", "description": "Testing.", "version": "1.0", "background_page": "background.html", "permissions": [ "tabs", "http://www.sitedomain.com/*" ], "content_scripts": [ { "matches": ["*://*.sitedomain.com/*"], "js": ["jquery.min.js", "test.js"], "css": ["test.css"] } ] } </code></pre> <p>Content Script:</p> <pre><code>$(document).ready(function () { alert('test js fired'); $("#ColumnContainer div.item").each(function () { $(this).css("background-color", "skyBlue"); var itemId = $(this).children("a.itemImage").attr("href"); $(this).children(".details").append("&lt;a href=\"javscript:void(false);\" onclick=\"gotoItem('" + itemId + "');\"&gt;Goto Item&lt;/a&gt;"); }); }); chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { alert('listener request'); alert(request); }); </code></pre> <p>JavaScript of Background HTML:</p> <pre><code> chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if (changeInfo.status == "complete") { if (tab.url.indexOf("sitedomain.com") &gt; -1) { chrome.tabs.executeScript(null, {file: "test.js"}); } } }); chrome.tabs.sendRequest(tabId, request, responseCallback); function responseCallback() { alert('response callback'); } function gotoItem(itemId) { alert('goto Item - ' + itemId); } </code></pre> <p>The above code does append the link and change the styling on the client page when the <code>sitedomain.com</code> is loaded. However, I haven't had any luck getting the <code>gotoItem</code> method to fire, Chrome Dev Tools shows undefined. I have tried various combinations, but just can't quite grasp the listeners and requests yet.</p> <p>I would really like to see a clean sample that just shows how to call a method from each site.</p>
    singulars
    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. 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