Заполнение всплывающего окна. html из фонового скрипта - PullRequest
0 голосов
/ 13 февраля 2020

Привет. Нужна помощь с этим

. Как только расширение загружено, содержимое всплывающего окна. html должно исходить из фонового скрипта (сторонний вызов API отдыха каждые 5 минут)

Здесь у меня есть 2 запроса:

  1. Заполнение всплывающего окна. html в первый раз (данные поступают из фонового скрипта)

  2. Заполняется каждые пять минут

Пожалуйста, посмотрите ниже, что у меня есть

background. js ------ ----

chrome.runtime.onInstalled.addListener(() => {
    console.log('alarm onInstalled...');
    // create alarm after extension is installed / upgraded
    chrome.alarms.create('refresh', { periodInMinutes: 1 });
  });
  
  chrome.alarms.onAlarm.addListener((alarm) => {
    console.log(alarm.name); // refresh
    fetchDataAPI();
  });
  
  function fetchDataAPI() {
    console.log("Hello, world!");
    if (jQuery) {  
        console.log("jQuery loaded!"); // jQuery loaded
    } else {
        console.log("jQuery not loaded!"); // jQuery not loaded
    }
    console.log(jQuery().jquery);
    console.log($().jquery);
    //jQuery('#pID')[0].innerHTML += "Hello, world!  ";
    //$('#pID')[0].innerHTML += "Hello, world!  ";
    //chrome.sendResponse({sendData: "data from api fetched due to alarm </br>"});
    //chrome.storage.sync.set({strDataAPI: "... data from api fetched due to alarm and stored in storage ... </br>"});
  }

  chrome.runtime.onMessage.addListener( function(request,sender,sendResponse)
  {
    if( request.btnClicked == "Refresh Now" )
    {
        console.log("Fetching Data from api ..");
        sendResponse({sendData: "data from api fetched ondemand </br>"});
    }
    return true;
  });

  chrome.runtime.onSuspend.addListener(function() {
    console.log("onSuspend Unloading...");
    chrome.browserAction.setBadgeText({text: "z"});
  });

  //chrome.browserAction.onClicked.addListener(function(tab) { alert('icon clicked')});


 

всплывающее окно. js -----------------

function getURL() {
    chrome.runtime.sendMessage({btnClicked: "Refresh Now"},
        function (response) {
            console.log("Response from background script ..." + response.sendData);
            $('#pID')[0].innerHTML = response.sendData;
        });
}

$(function()
{
    $('#GetAlertsId').click(function()
    {
        getURL();
    });
});

window.onload = function() {
    console.log("popup onload ..." + Date())
    /*
    chrome.storage.sync.get(['strDataAPI'], function(result) {
        console.log('Value currently is ' + result.strDataAPI);
        $('#pID')[0].innerHTML = result.strDataAPI;
    });
    */
}

всплывающее окно. html ----------

<!doctype html>
<html>
<head>
<title>Alarms Popup</title>
<script src="jquery-3.4.1.min"> </script>
<script src="popup.js"></script>
</head>
<body>
    <p id="pID"> </p>
    <input type="submit" id="GetAlertsId" value="GetAlerts">
</ul>
</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...