首页 > 解决方案 > How to store and retrieve an undefined guid with Javascript?

问题描述

My chrome extension reads an RSS feed that is updated quite often with new posts. What I want to do is assign every guid to an array that holds and then helps determine whether or not each post is new, and so ought to be passed on (as a notification to me).

    $.get('rss-feed', function(data) {
    var $xml = $(data);
    $xml.find("item").each(function() {
        var $this = $(this),
            item = {
                title: $this.find("title").text(),
                link: $this.find("link").text(),
                description: $this.find("description").text(),
                pubDate: $this.find("pubDate").text(),
                guid: $this.find("guid").text()
        }

The guid is then assigned to a variable:

    var latestID = item.guid.slice(-8);

This is where the problem takes place. The very same post keeps being posted as a new one. Is it because it has an undefined value?

    if (latestID == ids[0]) {

    } else if (latestID != ids[0]) {

        var firstNotification = {
            type: "basic",
            title: jobType[0] + ": " + jobWords[1] + " words",
            iconUrl: "icon48.png",
            message: jobPay[0] + "\nID: " + latestID + " Date: " + item.pubDate.slice(17,25),
            requireInteraction: true,
            buttons: [{
                    title: "Go to site!"
                }],
        };

        latestID = ids[0];
    }

标签: javascriptarraysgoogle-chromegoogle-chrome-extensionguid

解决方案


您可以使用本地存储将值保存在本地并检查

window.localStorage.setItem("somename",item.guid.slice(-8));

然后你可以在任何你想使用的地方检索,

var prevId = window.localStorage.getItem("somename");

你现在可以比较


推荐阅读