完全不知道如何保存扩展弹出窗口内容

IT技术 javascript html google-chrome google-chrome-extension firefox-addon-webextensions
2021-03-01 18:47:57

每次打开新链接或单击“离开”时,如何使弹出窗口的添加内容不消失,我几乎迷失了方向。我已经阅读了内容脚本、背景脚本等内容,但老实说我不知道​​如何将其实现到我自己的源代码中。下面是我的popup.htmlpopup.js和我的manifest.js文件。

{
    "manifest_version": 2,
    "name": "URL_save",
    "description": "This extension saves an URL and renames the title to the user's wishes and hyperlink the title.",
    "version": "0.1",

    "browser_action": {
        "default_icon": "/img/icon.png",
        "default_popup": "popup.html",
        "default_title": "See your saved websites!"
    },

    "permissions": [
        "tabs"
    ]
}

弹出 html

<html>
  <head>
    <title>Your articles</title>
    <link href="/css/style.css" rel="stylesheet"/>
    <script src="/js/underscore-min.js"></script>
    <script src="/js/popup.js"></script>
  </head>
  <body>
    <div id="div">No content yet! Click the button to add the link of the current website!</div>
    <div><ul id="list"></ul></div>
    <br/>
    <button id="button">Add link!</button>
  </body>
</html>

popup.js :

// global variables
var url;

// event listener for the button inside popup window
document.addEventListener('DOMContentLoaded', function() {
    var button = document.getElementById('button');
    button.addEventListener('click', function() {
        addLink();
    });
});

// fetch the URL of the current tab, add inside the window
function addLink() {
// store info in the the queryInfo object as per: 
//   https://developer.chrome.com/extensions/tabs#method-query
    var queryInfo = {
    currentWindow: true,
    active: true
    };

    chrome.tabs.query(queryInfo, function(tabs) {
        // tabs is an array so fetch the first (and only) object-elemnt in tab
        // put URL propery of tab in another variable as per: 
        //   https://developer.chrome.com/extensions/tabs#type-Tab
        url = tabs[0].url;

        // format html
        var html = '<li><a href=' + url + " target='_blank'>" + url + '</a><br/></li>';

        // change the text message
        document.getElementById("div").innerHTML = "<h2>Saved pages</h2>";

        // get to unordered list and create space for new list item 
        var list = document.getElementById("list");
        var newcontent = document.createElement('LI');
        newcontent.innerHTML = html;

        // while loop to remember previous content and append the new ones
        while (newcontent.firstChild) {
            list.appendChild(newcontent.firstChild);
        }
    });
}

在此图像中,您可以看到当我第一次添加链接但随后关闭(仅)弹出窗口并再次打开它时会发生什么:

添加当前 URL 后:
前

关闭并重新打开弹出窗口后:
后

2个回答

与网页类似,弹出窗口(或选项/设置页面)的范围在显示时创建,在不再可见时销毁。这意味着在弹出窗口显示的时间之间没有存储在弹出窗口本身中的状态。您希望在弹出窗口被销毁后保留的任何信息,您都需要存储在其他地方。因此,您将需要使用 JavaScript 来存储您希望在下次打开弹出窗口时保持相同的任何状态。每次打开弹出窗口时,您都需要检索该信息并将其恢复到 DOM。两个最常用的地方是StorageArea MDN或背景页面。

您存储信息的位置取决于您希望存储的数据保留多长时间,以及您希望数据显示的位置。

您可以存储数据的一般位置包括(存在其他可能性,但以下是最常见的):

  • 如果您希望数据仅在 Chrome 关闭之前存在,则为后台页面。一旦 Chrome 重新启动,它将不存在。您可以通过几种/几种不同的方法将数据发送到后台页面,包括消息传递MDN,或直接更改后台页面MDN上的值存储在 StorageArea(以下两个选项)中的数据也可用于后台页面和内容脚本。
  • chrome.storage.local如果您希望数据在关闭和重新启动的 Chrome 中保留在本地计算机上,请使用MDN
  • chrome.storage.syncMDN如果您希望与使用当前 Chrome 帐户/配置文件的所有 Chrome 实例共享数据。数据也将保持不变,直到更改。它将通过关闭和重新启动 Chrome 来使用。它将在使用相同配置文件的其他机器上可用。
  • window.localStorage:在存在之前chrome.storage,将扩展的数据存储在window.localStorage. 虽然这仍然有效,但通常首选使用chrome.storage.

使用chrome.storage StorageArea MDN的优点之一是数据可直接用于扩展的所有部分,而无需将数据作为消息传递。1

您当前的代码

目前,您的代码没有存储在弹出窗口的 DOM 之外的任何地方输入的 URL。您将需要建立一个数据结构(例如一个数组)来存储 URL 列表。然后可以将该数据存储到上述存储位置之一。

Google 在选项文档页面2上的示例MDN显示chrome.storage.sync了在显示选项页面时将值存储和恢复到 DOM 中。在这个例子中使用的代码可以在选项页面可以准确地工作,用于开发一个弹出的只是定义其HTML页面的default_popup一个browser_action还有许多其他示例可用。

不幸的是,如果您没有提供更多关于您想要什么的细节,就很难为您提供具体的代码。但是,朝着您需要前进的方向前进的一些建议是:

  • 重构您的代码,以便您拥有一个单独的函数,您可以使用 URL 作为参数调用该函数,该函数只是将此 URL 添加到您在 DOM 中的列表中(例如addUrlToDom(url))。当用户添加 URL 以及在页面加载时恢复 URL 时,将使用此函数。
  • 将您的 URL 列表存储在一个数组中(例如urlList)。此数组将是您保存到弹出窗口之外的存储位置的内容。您将从DOMContentLoaded处理程序中的该存储位置读取此数组,并使用重构的addUrlToDom()函数添加每个值。将其恢复到 DOM 可能类似于:

    urlList.forEach(function(url){
        addUrlToDom(url);
    });
    

将您的数据存储在 chrome.storage.local

假设您想通过 Chrome 关闭/重启(即使用chrome.storage.local将 URL 存储在本地计算机上,您的代码可能如下所示:

manifest.json更改为permissions仅:

    "permissions": [
        "tabs",
        "storage"
    ]

popup.js :

// global variables
var urlList=[];

document.addEventListener('DOMContentLoaded', function() {
    getUrlListAndRestoreInDom();
    // event listener for the button inside popup window
    document.getElementById('button').addEventListener('click', addLink);
});

// fetch the URL of the current tab, add inside the window
function addLink() {
    chrome.tabs.query({currentWindow: true,active: true}, function(tabs) {
        // tabs is an array so fetch the first (and only) object-element in tab
        var url = tabs[0].url;
        if(urlList.indexOf(url) === -1){
            //Don't add duplicates
            addUrlToListAndSave(url);
            addUrlToDom(url);
        }
    });
}

function getUrlListAndRestoreInDom(){
    chrome.storage.local.get({urlList:[]},function(data){
        urlList = data.urlList;
        urlList.forEach(function(url){
            addUrlToDom(url);
        });
    });
}

function addUrlToDom(url){
    // change the text message
    document.getElementById("div").innerHTML = "<h2>Saved pages</h2>";

    //Inserting HTML text here is a bad idea, as it has potential security holes when
    //  including content not sourced entirely from within your extension (e.g. url).
    //  Inserting HTML text is fine if it is _entirely_ sourced from within your
    //  extension.
    /*
    // format HTML
    var html = '<li><a href=' + url + " target='_blank'>" + url + '</a></li>';
    //Add URL to DOM
    document.getElementById("list").insertAdjacentHTML('beforeend',html);
    */
    //Build the new DOM elements programatically instead:
    var newLine = document.createElement('li');
    var newLink = document.createElement('a');
    newLink.textContent = url;
    newLink.setAttribute('href',url);
    newLink.setAttribute('target','_blank');
    newLine.appendChild(newLink);
    document.getElementById("list").appendChild(newLine);
}

function addUrlToListAndSave(url){
    if(urlList.indexOf(url) === -1){
        //URL is not already in list
        urlList.push(url);
        saveUrlList();
    }
}

function saveUrlList(callback){
    chrome.storage.local.set({urlList},function(){
        if(typeof callback === 'function'){
            //If there was no callback provided, don't try to call it.
            callback();
        }
    });
}

  1. 例外情况是您插入到页面上下文中的脚本。页面上下文是您可能不会在其中运行脚本的内容。为此,您必须使用内容脚本(您的StorageArea MDN数据可直接在其中使用)将<script>标签插入到网页的 DOM 中。这可能有点复杂,您可能不需要担心它。在这里提到它仅仅是因为StorageArea MDN数据可用于扩展的所有区域的声明可能存在例外
  2. Chrome 文档中的示例在 Firefox 上运行良好。是的,Firefox 支持chrome.*使用回调和browser.*使用 promise 的
是的,回调通常是一个函数(CB),它被传递给另一个函数(S),目的是一旦 S 应该做的动作完成就调用 CB。在这段代码中,saveUrlList()有能力接受一个可选的回调,一旦urlList保存将被调用使用if(typeof callback === 'function'){测试来查看回调函数是否作为callback. 如果没有传递任何函数,那么我们不会尝试调用它。addUrlToListAndSave保存完成后没有任何必须做的事情,所以没有传递回调函数。(续)
2021-04-16 18:47:57
(cont')可以删除整个if语句块saveUrlList()当前功能不需要它,但将来可能需要它。在代码中具有该功能主要是为了在任何其他代码中允许它(例如,我在答案中链接的选项示例使用set()回调来通知用户选项已更改)。使用回调可能需要,也可能不需要,但是saveUrlList()当您实现从列表中删除 URL 的方法时,您可能希望使用该方法(这本身并不需要该功能)。
2021-04-27 18:47:57
哇……毫无疑问,你真的很棒。你真是个天才。我需要了解的东西太多了,感谢您为我指明了正确的方向,甚至给了我更好的代码!感谢您的帮助,您对我的帮助超出了我的能力(并且会)要求!我真的很高兴,谢谢!
2021-05-01 18:47:57
但是我确实有一个问题,在最后一个函数“saveUrlList”中,它将回调作为参数。但是在“addUrlToListAndSave”函数中,“saveUrlList”不接收任何参数。不是定义如下的回调函数:“嘿(不管名称)函数,当你完成你的工作时给我打电话,并给我(可选)你工作的输出”。然后前面的函数,比如 chrome.storage.local.set 调用了无名函数。但我不明白为什么需要最后一个函数以及它是如何工作的。developer.chrome.com/extensions/storage 上,我发现它有点含糊。
2021-05-02 18:47:57
明白了,谢谢!我现在几乎理解了所有的代码。但是我没有得到关于 developer.chrome 站点的 chrome.storage.local.set 的这句话:“一个对象,它为每个键/值对提供更新存储。存储中的任何其他键/值对都不会受到影响.” 它们是否意味着(在我们的上下文中)当您提供一个对象(内部是 urlList 数组)时,如果其中的 url 已经存在,则不会将其设置到存储中我也没有得到:“chrome.storage.local.get( {urlList : [] }, ....” 第二个函数的一部分。为什么数组必须在对象内部?
2021-05-04 18:47:57

每次关闭/重新打开时,弹出窗口都会重新加载它的文档。相反,您应该使用背景页面来保留状态。

这样做的一般方法是:

  1. 从弹出窗口到后台进行通信以做某事。即:执行 ajax 请求并将结果存储在一个普通的 javascript 对象中。
  2. 从后台页面获取,以获取 javascript 对象。即使您关闭了弹出窗口,状态仍会保留在后台页面中。