Как сохранить данные из файла popup.js в расширении Chrome до IndexedDB - PullRequest
0 голосов
/ 06 июня 2018

У меня есть расширение Chrome, над которым я работаю.Мне нужно иметь возможность хранить данные из всплывающего окна в IndexedDB в Chrome, чтобы я мог получить к ним доступ позже.Я очень новичок в этом, и мне нужна пошаговая помощь.

manifest.json

{
  "manifest_version": 2,

  "name": "BrowseMonitor",
  "description": "Tracks how much time spent on a particular web page",
  "version": "1",
  "author": "youngj@fit.edu",

  "permissions": [
    "tabs"
    "storage"

  ],
  "icons": {
    "16": "clock-icon16.png",
    "32": "clock-icon32.png",
    "76": "clock-icon76.png",
    "152": "clock-icon152.png"
  },
  "browser_action": {
    "default_icon": {
     "64": "icons8-watch-64.png"
    }
  },
  "background": {
    "scripts": [
      "common.js",
      "background.js"
      ],
    "persistent": true
  },
  "options_page": "options.html"
}

background.js

var History = {};

chrome.browserAction.setBadgeText({ 'text': '?'});
chrome.browserAction.setBadgeBackgroundColor({ 'color': "#663" });

function Update(t, tabId, url) {
  if (!url) {
    return;
  }
  if (tabId in History) {
    if (url == History[tabId][0][1]) {
      return;
    }
  } else {
    History[tabId] = [];
  }
  History[tabId].unshift([t, url]);

  var history_limit = parseInt(localStorage["history_size"]);
  if (! history_limit) {
    history_limit = 100;
  }
  while (History[tabId].length > history_limit) {
    History[tabId].pop();
  }

  chrome.browserAction.setBadgeText({ 'tabId': tabId, 'text': '0:00'});
  chrome.browserAction.setPopup({ 'tabId': tabId, 'popup': "popup.html#tabId=" + tabId});
}

function HandleUpdate(tabId, changeInfo, tab) {
  Update(new Date(), tabId, changeInfo.url);
}

function HandleRemove(tabId, removeInfo) {
  delete History[tabId];
}

function HandleReplace(addedTabId, removedTabId) {
  var t = new Date();
  delete History[removedTabId];
  chrome.tabs.get(addedTabId, function(tab) {
    Update(t, addedTabId, tab.url);
  });
}


function UpdateBadges() {
  var now = new Date();
  for (tabId in History) {
    var description = FormatDuration(now - History[tabId][0][0]);
    chrome.browserAction.setBadgeText({ 'tabId': parseInt(tabId), 'text': description});
  }
}

setInterval(UpdateBadges, 1000);

chrome.tabs.onUpdated.addListener(HandleUpdate);
chrome.tabs.onRemoved.addListener(HandleRemove);
chrome.tabs.onReplaced.addListener(HandleReplace);

popup.js

var tabId_re = /tabId=([0-9]+)/;
var match = tabId_re.exec(window.location.hash);
if (match) {
  var hist = chrome.extension.getBackgroundPage().History[match[1]];
  var table = document.createElement("table");
  for (var i=0; i < hist.length; i++) {
    var firstRow = table.insertRow(-1);

    var date = "";
    if (i == hist.length - 1 ||
        (hist[i][0].toLocaleDateString() != hist[i+1][0].toLocaleDateString())) {
      date = hist[i][0].toLocaleDateString();
    }
    firstRow.insertCell(-1).textContent = date;

    firstRow.insertCell(-1).textContent = hist[i][0].toLocaleTimeString();

    var end_time;
    if (i == 0) {
      end_time = new Date();
    } else {
      end_time = hist[i-1][0];
    }
    firstRow.insertCell(-1).textContent = FormatDuration(end_time - hist[i][0]);

    var a = document.createElement("a");
    a.textContent = hist[i][1];
    a.setAttribute("href", hist[i][1]);
    a.setAttribute("target", "_blank");
    firstRow.insertCell(-1).appendChild(a);

    var secondRow = table.insertRow(-1);

    secondRow.insertCell(-1).textContent = 'Date';
    secondRow.insertCell(-1).textContent = 'Time Started on Site';
    secondRow.insertCell(-1).textContent = 'Time Spent';
    secondRow.insertCell(-1).textContent = 'Site URL';
    secondRow.insertCell(-1).textContent = 'Category';
  }
  table.className = 'table'
  document.body.appendChild(table);

 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        var port = chrome.tabs.connect(tabs[0].id,{name: "extension_request"});
            port.postMessage({db: "database_name_example"}); // send database name
            port.onMessage.addListener(function(msg) {
              if (msg.foo ) {
              }
           }
}

Мне просто нужно знать, как сделать дампвсе данные, которые появляются во всплывающем окне базы данных IndexedDB.

...