0

hi i have a function at background.js

function cookielog()
{
chrome.cookies.getAll({},function(c){console.log(c);});
}

i want to call this cookielog from any webpage

i have seen this

sending message to chrome extension from a web page

but it didn't work for me, i get this error

attempting to use a disconnected port object

in my webpage's execute js

var customEvent = document.createEvent('Event');
customEvent.initEvent('cookieyolla', true, true);
function fireCustomEvent(data) {
    hiddenDiv = document.getElementById('cookieyolla');
    hiddenDiv.innerText = data;
    hiddenDiv.dispatchEvent(customEvent);
}
fireCustomEvent(5);

background.js

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (tab.url.indexOf('http') == '0' && changeInfo.status == 'complete') {
    try {
        chrome.tabs.executeScript(tabId, {
            code: 'if(typeof onceworked==\'undefined\'){var onceworked=true;var port=chrome.extension.connect();port.onMessage.addListener(function(msg){if(msg.message==\'cookieyolla\'){setTimeout(function(){alert(\'sik beni\');chrome.cookies.getAll({},cookielog);},msg.values*1000);}});var iDiv=document.createElement(\'div\');iDiv.id=\'cookieyolla\';iDiv.style.display=\'none\';document.getElementsByTagName(\'body\')[0].appendChild(iDiv);document.getElementById(\'cookieyolla\').addEventListener(\'cookieyolla\',function(){port.postMessage({message:\'cookieyolla\',values:document.getElementById(\'cookieyolla\').innerText});});};',
            allFrames: true,
            runAt: 'document_end'
        });
    } catch (e) {
    }
}

});

what i am doing wrong?

if (typeof onceworked == 'undefined') {
    var onceworked = true;
    var port = chrome.extension.connect();
    port.onMessage.addListener(function (msg) {
        if (msg.message == 'cookieyolla') {
            setTimeout(function () {
                alert('sik beni');
                chrome.cookies.getAll({}, cookielog);
            }, msg.values * 1000);
        }
    });
    var iDiv = document.createElement('div');
    iDiv.id = 'cookieyolla';
    iDiv.style.display = 'none';
    document.getElementsByTagName('body')[0].appendChild(iDiv);
    document.getElementById('cookieyolla').addEventListener('cookieyolla', function () {
        port.postMessage({
            message : 'cookieyolla',
            values : document.getElementById('cookieyolla').innerText
        });
    });
};

code:"" part as formatted

best regards

Haibara Ai
  • 10,703
  • 2
  • 31
  • 47

1 Answers1

0

@Mustafa, you should read though long-live connection.

Sometimes it's useful to have a conversation that lasts longer than a single request and response. In this case, you can open a long-lived channel from your content script to an extension page , or vice versa, using runtime.connect or tabs.connect, respectively . The channel can optionally have a name, allowing you to distinguish between different types of connections.

Also, try using Dev Tools. This will help you pause the action on uncaught errors/exceptions.

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91