Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展:避免重复?_Javascript_Json_Google Chrome_Google Chrome Extension_Xmlhttprequest - Fatal编程技术网

Javascript Chrome扩展:避免重复?

Javascript Chrome扩展:避免重复?,javascript,json,google-chrome,google-chrome-extension,xmlhttprequest,Javascript,Json,Google Chrome,Google Chrome Extension,Xmlhttprequest,每次扩展刷新时,它都复制整个feed.json。如何防止这种情况发生,并仅在旧项目的基础上添加来自feed.json(更新后)的新项目?另外,如何仅在添加新项目时设置badgeText 以下是我目前得到的信息: background.html <html> <head> <script type="text/javascript"> var fetchFreq = 30000; // how often we fetch new ite

每次扩展刷新时,它都复制整个
feed.json
。如何防止这种情况发生,并仅在旧项目的基础上添加来自
feed.json
(更新后)的新项目?另外,如何仅在添加新项目时设置
badgeText

以下是我目前得到的信息:

background.html

<html>
  <head>
    <script type="text/javascript">
      var fetchFreq = 30000; // how often we fetch new items (30s)
      var req; // request object
      var unreadCount = 0; // how many unread items we have
      var items; // all currently fetched items

      getItems();
      setInterval(getItems, fetchFreq);

      function getItems() {
        req = new XMLHttpRequest();
        req.open('GET', 'http://siteurl.com/feed.json');
        req.onload = processItems;
        req.send();
      }

      function processItems() {
        var res = JSON.parse(req.responseText);
        unreadCount += res.length;

        if (unreadCount > 0) {
          chrome.browserAction.setBadgeBackgroundColor({
            color: [255, 0, 0, 255]
          });
          chrome.browserAction.setBadgeText({text: '' + unreadCount});
        }

        items = res.concat(items);
      }
    </script>
  </head>
</html>
<html>
  <head>
    <link rel="stylesheet" href="popup.css" />
    <script src="util.js"></script>
    <script>
      var bg; // background page

      // timeline attributes
      var timeline;
      var template;
      var link;
      var image;
      var author;
      var content;

      onload = setTimeout(init, 0); // workaround for http://crbug.com/24467

      // initialize timeline template
      function init() {
        chrome.browserAction.setBadgeText({text: ''});
        bg = chrome.extension.getBackgroundPage();
        bg.unreadCount = 0;

        timeline = document.getElementById('timeline');
        template = xpath('//ol[@id="template"]/li', document);
        link = xpath('//div[@class="thumbnail"]/a', template);
        image = xpath('img', link);
        author = xpath('//div[@class="text"]/a', template);
        content = xpath('//div[@class="text"]/span', template);

        update();
      }

      // update display
      function update() {
        var user;
        var item;

        for (var i in bg.items) {
          user = bg.items[i];

          // thumbnail
          link.title = user.name;
          link.href = openInNewTab(profileurl);
          image.src = user.thumbnail;
          image.alt = user.name;

          // text
          author.href = openInNewTab(profileurl);
          author.innerHTML = user.name;
          content.innerHTML = linkify(bg.items[i].profileurl);

          // copy node and update
          item = template.cloneNode(true);
          timeline.appendChild(item);
        }
      }
    </script>
  </head>
  <body>
    <div id="body">
      <div id="title">
        <h2>Chrome Extension</h2>
      </div>
      <ol id="timeline" />
    </div>
    <ol id="template">
      <li>
        <div class="thumbnail">
          <a>
            <img />
          </a>
        </div>
        <div class="text">
          <a></a>
          <span></span>
        </div>
        <div class="clear"></div>
      </li>
    </ol>
  </body>
</html>

提前谢谢。扩展的目的是从feed.json获取新项目并显示在popup.html中。把它想象成新推特推特的提要阅读器。

我知道现在看起来不错,但拥有一个全局
req
对象可能不如为每个请求拥有一个对象安全

您似乎从未清除过
。你的弹出窗口和背景应该是沟通的。背景应该分别保存新的和旧的提要,当弹出窗口显示它们时,应该释放它们

例如,考虑将旧项保存在一个数组中,将新项保存在一个单独的数组中。然后,当弹出窗口向用户显示新的提要时,它可以调用
bg.clearItems()
,将新提要复制到旧提要的数组中,然后在下一次请求时,新提要被放入现在为空的新提要数组中

{
"name":"John Doe",
"about":"I am me",
"thumbnail":"http://thumbnail.jpg",
"profileurl":"http://siteurl.com/profileurl.php"
}