Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome extension to Send Message from popup to content script

I,am developing an extension in which i have to extract data from linkedin profile page when user press button on popup. I,am passing message from the popup.js page to contentscript and in response i will get data extracted from linkedin profile page by contentscript so that i can display it in popup.html. But I,am getting error when i inspected the popup.html. The error is:

Port: Could not establish connection. Receiving end does not exist. lastError:29
Error in event handler for 'undefined': Cannot read property 'farewell' of undefined
TypeError: Cannot read property 'farewell' of undefined
    at chrome-extension://kdfgoafjicddfffdbfofdmckejemfije/popup.js:6:25
    at <error: illegal access>
    at Event.dispatchToListener (event_bindings:356:21)
    at Event.dispatch_ (event_bindings:342:27)
    at Event.dispatch (event_bindings:362:17)
    at Object.chromeHidden.Port.dispatchOnDisconnect (miscellaneous_bindings:258:27)

For reference, my manifest file is:

{
    "name": "SoftwareGrid",
    "version": "0.5",
    "icons": { "16": "icons/16.png","48": "icons/48.png", "128": "icons/128.png" },
    "description": "Shows user cresidentials on Linkedin",
    "permissions": [
        "cookies",
        "tabs",
        "http://www.linkedin.com/*"
    ],

    "browser_action": {
        "default_title": "Show Profile",
        "default_icon": { "16": "icons/16.png","48": "icons/48.png", "128": "icons/128.png" },
        "default_popup": "popup.html"
    },

    "background": {
        "scripts": ["jquery-1.7.2.min.js","background.js"]
    }, 

    "content_scripts": [{
        "matches": ["http://www.linkedin.com/*"],
        "all_frames": true,
        "js": ["jquery-1.7.2.min.js", "script.js"],
        "run_at": "document_end"
    }],

    "web_accessible_resources": [
        "icons/i1.png"
    ],

    "manifest_version": 2
}

My popup.js file:

function sendClicks() {
    console.log("popup.js > sendClicks()");

    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
            console.log(response.farewell);
        });
    });

    console.log("avra' inviato?");
}

$(function() {
    console.log("popup.js > OMD Extension ready");
    $('#sendclicks').click(function(){
        sendClicks();
    });
});

My contentscript file

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");

        if (request.greeting == "hello")
            sendResponse({farewell: "goodbye"});
});

Plz help!

like image 376
saadsaf Avatar asked Oct 05 '13 12:10

saadsaf


People also ask

How do I send a message from content script to popup script?

If you are sending messages from content script to popup, then you just need to use chrome. runtime. sendMessage instead of tabs. sendMessage .

Can Chrome extensions communicate with each other?

In addition to sending messages between different components in your extension, you can use the messaging API to communicate with other extensions.

What is content script Chrome extension?

A content script is a part of your extension that runs in the context of a particular web page (as opposed to background scripts which are part of the extension, or scripts which are part of the website itself, such as those loaded using the <script> element).

How do I open popup content script?

They've set the default shortcut to Ctrl+D . Activating this command will perform a click on the page (or browser) action, opening the popup or whatever the action is configured to do.


1 Answers

You may have to add this in your manifest:

"permissions" : ["tabs"]
like image 109
anipendakur Avatar answered Sep 21 '22 02:09

anipendakur