1

I need some help with my code. Im trying to messaging from background.js to content-script on web-page but something got wrong

manifest.json

{
    "name": "Fill my password",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Fills my password on University login page",
    "background": 
    {
        "scripts": ["background.js"]
    },
    "permissions": ["tabs", "<all_urls>", "contextMenus"],
    "content_scripts": [
    {
        "matches": ["https://www.binary.com/ru/trading.html?currency=AUD&market=volidx&underlying=R_10&formname=risefall&date_start=now&duration_amount=1&duration_units=m&amount=10&amount_type=payout&expiry_type=duration"],
        "js": ["content.js"]
    }]
}

content.js

{
    var loginForm = document.getElementById('btn_login');
    chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {
        alert("Message recieved!");
        if (msg.action == "open_dialog_box") {
            loginForm.click();
            alert("Message recieved!");
        }
    });
}

background.js

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    var time = new Date();
    secondsRemaining = (60 - time.getSeconds()) * 1000;
    setTimeout(
        function () {
            chrome.tabs.sendMessage(tabs[0].id, { action: 'open_dialog_box' },
                alert('response'))
        }, 5000
    )
})

Thanks for your help!

Waleed Iqbal
  • 1,308
  • 19
  • 35
  • What is going wrong? Please describe the observed behavior and the desired behavior. If someone tries to assess your code, that person might find everything in order, as it behaves like he (or she) expects. – derM - not here for BOT dreams Nov 28 '17 at 10:53
  • After 5 sec need to alert and then at open http(like in manifest) change page to login_page form button_click, but that dont happen. I trying to find resolve but after 5 hours i dont find what wrong. When i try debugging extension the error is with tab_id – AntiSoprano Nov 28 '17 at 11:44
  • 1. don't use `alert`, it breaks the synchronicity of the callback, use` console.log` (background page has its [own console](https://stackoverflow.com/a/10258029)) 2. if onMessage callback is doing something asynchronously you need to add `return true` at the end of the callback – wOxxOm Nov 28 '17 at 20:24

0 Answers0