Добавляйте теги alt и title к изображениям с помощью JavaScript - PullRequest
0 голосов
/ 20 марта 2020

У меня есть этот скрипт, который использует канал блоггера для извлечения контента.

Он извлекает текст и изображение из статьи.

Было бы идеально, если бы были добавлены теги Alt и Title к изображениям, кто-нибудь знает, как это сделать?

<div id="feed-list-container">
</div>
<div style="clear:both;">
</div>

<style>
div[data-tag=''] {float: left;

    positon: relative;
    width: 26%;

    background-color: #9cb5c2;
}

</style>

<script type='text/javascript'>
var multiFeed = {
    feedsUri: [

        {
            name: "Terror",
            url: "https://elfenliedbrazil.blogspot.com/",
            tag: "Terror"
        },

    ],
    numPost: 18,
    showThumbnail: true,
    showSummary: true,
    summaryLength: 200,
    titleLength:"auto",
    thumbSize: 200,
    containerId: "feed-list-container",
    readMore: {
        text: "",
        endParam: "?max-results=20"
    }
};
</script>

Это мой код до сих пор:

var mf_defaults = {
  feedsUri: [{
    name: "Posting JQuery",
    url: " ",
    tag: "JQuery"
  }, {
    name: "Posting CSS",
    url: "",
    tag: "CSS"
  }, {
    name: "Widget-Widget Blogger",
    url: " ",
    tag: "Widget"
  }],
  numPost: 4,
  showThumbnail: true,
  showSummary: true,
  summaryLength: 80,
  titleLength: "auto",
  thumbSize: 200,
  thumbWidth: 200, // new setting
  thumbHeight: 90, // new setting
  newTabLink: false,
  containerId: "feed-list-container",
  listClass: "list-entries",
  readMore: {
    text: "More",
    endParam: "?max-results=20"
  },
  autoHeight: false,
  current: 0,
  onLoadFeed: function(a) {},
  onLoadComplete: function() {},
  loadFeed: function(c) {
    var d = document.getElementsByTagName("head")[0],
      a = document.getElementById(this.containerId),
      b = document.createElement("script");
    b.type = "text/javascript";
    b.src = this.feedsUri[c].url + "/feeds/posts/summary" + (this.feedsUri[c].tag ? "/-/" + this.feedsUri[c].tag : "") + "?alt=json-in-script&max-results=" + this.numPost + "&callback=listEntries";
    d.appendChild(b)
  }
};
for(var i in mf_defaults) {
  mf_defaults[i] = (typeof(multiFeed[i]) !== undefined && typeof(multiFeed[i]) !== "undefined") ? multiFeed[i] : mf_defaults[i]
}

function listEntries(q) {
  var p = q.feed.entry,
    c = mf_defaults,
    h = document.getElementById(c.containerId),
    a = document.createElement("div"),
    d = "",
    l = c.feedsUri.length,
    n, k, m, g;
  for(var f = 0; f < c.numPost; f++) {
    if(f == p.length) {
      break
    }
    n = (c.titleLength !== "auto") ? p[f].title.$t.substring(0, c.titleLength) + (c.titleLength < p[f].title.$t.length ? "&hellip;" : "") : p[f].title.$t;
    m = ("summary" in p[f]) ? p[f].summary.$t.replace(/<br ?\/?>/g, " ").replace(/<.*?>/g, "").replace(/[<>]/g, "") : "";
    m = (c.summaryLength < m.length) ? m.substring(0, c.summaryLength) + "&hellip;" : m;
    g = ("media$thumbnail" in p[f]) ? '<img src="' + p[f].media$thumbnail.url.replace(/\/s72(\-c)?\//, "/w" + c.thumbWidth + "-h" + c.thumbHeight + "-c/") + '" style="width:' + c.thumbWidth + "px;height:" + c.thumbHeight + 'px;">' : '';
    for(var e = 0, b = p[f].link.length; e < b; e++) {
      k = (p[f].link[e].rel == "alternate") ? p[f].link[e].href : "#"
    }
    d += '<div data-tag="'+c.feedsUri[c.current].tag+'" class="post hentry"' + (!c.autoHeight ? ' style="height' + c.thumbHeight + 'px;overflow:hidden; border-width:1px; border-style: solid; border-color: #000000; height: 200px"' : "") + ">";
    d += '<div class="post-title-2" style=" height: 38px;"><h2 style=" font-size: 13px; "><a href="' + k + '"' + (c.newTabLink ? ' target="_blank"' : "") + ">" + n + "</a></h2></div>";
    d += (c.showThumbnail) ? g : "";
    d += '<div class="summary">';
    d += "<span" + (!c.showSummary ? ' style="display:none;"' : "") + ">";
    d += (c.showSummary) ? m : "";
    d += "</span></div>";
    d += '<span style="display:block;clear:both;"></span></div>'
  }
  d += "";
  d += '<div class="more-link"><a href="' + c.feedsUri[c.current].url.replace(/\/$/, "") + "/search/label/" + c.feedsUri[c.current].tag + c.readMore.endParam + '"' + (c.newTabLink ? ' target="_blank"' : "") + ">" + c.readMore.text + "</a></div>";
  a.className = c.listClass;
  a.innerHTML = '<div data-tag=" margin-top: -33px;'+c.feedsUri[c.current].tag+'" +class="main-title"><h2>' + c.feedsUri[c.current].name + "</h2></div>" + d;
  h.appendChild(a);
  c.onLoadFeed(c.current);
  if((c.current + 1) < l) {
    c.loadFeed(c.current + 1)
  }
  if((c.current + 1) == l) {
    c.onLoadComplete()
  }
  c.current++
}
mf_defaults.loadFeed(0);

Я считаю, что сторона, которая несет ответственность за вытягивание изображение img

1 Ответ

1 голос
/ 20 марта 2020

Это может быть глупый ответ ... но насколько я понимаю, твой вопрос, ты пробовал это?

g = ("media$thumbnail" in p[f]) ? '<img src="' + p[f].media$thumbnail.url.replace(/\/s72(\-c)?\//, "/w" + c.thumbWidth + "-h" + c.thumbHeight + "-c/") + '" style="width:' + c.thumbWidth + "px;height:" + c.thumbHeight + 'px;" title="' + n + '" alt="' + n + '">' : '';
...