【问题标题】:Update chrome.storage if extension's file changes如果扩展的文件更改,则更新 chrome.storage
【发布时间】:2016-02-19 02:07:45
【问题描述】:

我有一个Chrome extension,它使用chrome.storage 来跟踪要应用于页面内容的样式表。其中一个样式表是必需的默认样式表,如果该文件在用户的chrome.storage 中不存在,我最初会从 Chrome 的扩展文件中加载该样式表。这很好用。

但是,我有时会使用不同的规则更新此默认样式表以改进样式。当扩展程序运行时,它会检查默认样式表是否存在并找到旧版本的样式表 - 因此它不会从扩展程序的存储中加载任何内容。因此用户仍在使用旧版本的样式表。

在我的本地计算机上,我可以手动清空我的 chrome.storage 并加载新的,但我无法在发布扩展程序时执行此操作,因为我不想在每次运行我的扩展程序时清空它我也不只知道样式表在 Chrome 的扩展文件中更新的时间。

我可以通过检查两个文件的每个字符,比较它们是否相同,如果是则加载扩展程序的样式表来解决这个问题,但这似乎有点矫枉过正并且容易出错。

有没有更简单的方法来更新 chrome.storage 的样式表,仅在扩展的样式表更新而不更改文件名的情况下?

如果你想看我的实现,整个项目是开源的on GitHub

【问题讨论】:

    标签: javascript google-chrome-extension auto-update file-management google-chrome-storage


    【解决方案1】:

    使用nudge from Florian in a chat,我想出了以下解决方案,使用第二个chrome.storage 空格。

    我已经在检查用户的 Chrome 存储中是否存在样式表,如果不存在,则从扩展程序的文件中加载样式表。为了让它在更改时自动更新,我现在检查第二个chrome.storage 空间,该空间在检查是否从 Chrome 的存储中加载样式表时保存版本号。基本做法如下:

    // Helper function that checks whether an object is empty or not
    function isEmpty(obj) {
        return Object.keys(obj).length === 0;
    }
    
    var stylesheetObj = {}, // Keeps track of all stylesheets
        stylesheetVersion = 1; // THIS NUMBER MUST BE CHANGED FOR THE STYLESHEETS TO KNOW TO UPDATE
    
    chrome.storage.sync.get('just-read-stylesheets', function (result) {
        // Here 'results' is an object with all stylesheets if it exists
    
        // This keeps track of whether or not the user has the latest stylsheet version
        var needsUpdate = false; 
    
        // Here I get the user's current stylesheet version
        chrome.storage.sync.get('stylesheet-version', function (versionResult) {
    
            // If the user has a version of the stylesheets and it is less than the cufrent one, update it
            if(isEmpty(versionResult) 
            || versionResult['stylesheet-version'] < stylesheetVersion) {
    
                chrome.storage.sync.set({'stylesheet-version': stylesheetVersion});
    
                needsUpdate = true;
            }
    
            if(isEmpty(result) // Not found, so we add our default
            || isEmpty(result["just-read-stylesheets"])
            || needsUpdate) { // Update the default stylesheet if it's on a previous version
    
                // Open the default CSS file and save it to our object
                var xhr = new XMLHttpRequest();
                xhr.open('GET', chrome.extension.getURL('default-styles.css'), true);
                    // Code to handle successful GET here
                }
                xhr.send();
                return;
            }
    
            // Code to do if no load is necessary here
        });
    });
    

    这使得为用户更新样式表唯一需要更改的是stylesheetVersion,确保它比以前的版本大。例如,如果我更新了样式表并希望用户的版本自动更新,我会将 stylesheetVersion1 更改为 1.1

    如果需要更完整的实现,可以找JS文件here on GitHub

    【讨论】:

      【解决方案2】:

      尝试使用chrome.storage.sync 并为其*onChanged* 事件添加一个侦听器。每当存储中发生任何变化时,都会触发该事件。以下是监听保存更改的示例代码:

      chrome.storage.onChanged.addListener(function(changes, namespace) {
          for (key in changes) {
              var storageChange = changes[key];
              console.log('Storage key "%s" in namespace "%s" changed. ' +
              'Old value was "%s", new value is "%s".',
              key,
              namespace,
              storageChange.oldValue,
              storageChange.newValue);
          }
      });
      

      【讨论】:

      • 这会监听 chrome 存储,但我的问题是当 extension 的 文件更改时我需要更新 chrome 的存储
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      相关资源
      最近更新 更多