aboutsummaryrefslogtreecommitdiff
path: root/static/js/aquiencitas.js
blob: 4284f0a0dcf5898b874bce67940d605fa566fd15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var cur_url;
var linklist;
var linki;
var is_bbs;
var plimit = 5;
function getPostRange(t, n) {
  var posts,
    replies,
    s,
    ss,
    ee,
    rev = false;
  posts = [];
  replies = t.getElementsByClassName("reply");
  s = n.split("-");
  ss = parseInt(s[0]);
  ee = ss;
  if (s.length == 2) ee = parseInt(s[1]);
  if (ee < ss) {
    tmp = ss;
    ss = ee;
    ee = tmp;
    rev = true;
  }
  for (j = 0; j < replies.length; j++) {
    num = parseInt(replies[j].dataset.n);
    if (num > ee) break;
    if (num >= ss && num <= ee) {
      if (rev) posts.unshift(replies[j]);
      else posts.push(replies[j]);
    }
  }
  return posts;
}
function findAncestor(el) {
  while (
    (el = el.parentElement) &&
    !el.className.startsWith("thread") &&
    !el.className.startsWith("cont")
  );
  return el;
}
function getPostDivs(e) {
  if (is_bbs) {
    divs = [];
    t = findAncestor(e);
    s = e.getAttribute("href").split("/");
    r = s[s.length - 1];
    rs = r.split(",");
    linki = 0;
    for (i = 0; i < rs.length; i++) {
      divs.push.apply(divs, getPostRange(t, rs[i]));
    }
    return divs;
  } else {
    ele = document.getElementById(
      "reply" + e.getAttribute("href").split("#")[1]
    );
    return [ele];
  }
}
function get_pid(e) {
  return is_bbs ? e.dataset.n : e.id.substr(5);
}
function fill_links(e) {
  var divs = getPostDivs(e);
  if (!divs[0]) return;

  this_id = get_pid(e.parentNode.parentNode);

  for (i = 0; i < divs.length; i++) {
    tid = get_pid(divs[i]);
    if (linklist[tid]) continue;
    if (this_id == tid) continue;
    t = is_bbs ? divs[i].getElementsByTagName("h4")[0] : divs[i];
    bl = document.createElement("a");
    bl.href = cur_url + (is_bbs ? "/" : "#") + this_id;
    bl.textContent = ">>" + this_id;
    bl.addEventListener("mouseover", who_are_you_quoting, false);
    bl.addEventListener("mouseout", remove_quote_preview, false);
    if (!(qb = t.getElementsByClassName("quoted")[0])) {
      qb = document.createElement(is_bbs ? "span" : "div");
      qb.className = "quoted";
      qb.textContent = " Citado por: ";
      if (is_bbs) {
        t.insertBefore(qb, t.getElementsByClassName("del")[0]);
        t.insertBefore(
          document.createTextNode(" "),
          t.getElementsByClassName("del")[0]
        );
      } else {
        p = t.getElementsByTagName("blockquote");
        p[p.length - 1].insertAdjacentHTML("afterend", qb.outerHTML);
      }
      t.getElementsByClassName("quoted")[0].appendChild(bl);
    } else {
      qb.appendChild(document.createTextNode(" "));
      qb.appendChild(bl);
    }
    linklist[tid] = true;
  }
}
function who_are_you_quoting(e) {
  var parent, d, clr, src, cnt, left, top, width, maxWidth;
  e = e.target || window.event.srcElement;
  var divs = getPostDivs(e);
  if (!divs[0]) return;

  maxWidth = 500;
  cnt = document.createElement("div");
  cnt.id = "q-p";
  width = divs[0].offsetWidth;
  if (width > maxWidth) {
    width = maxWidth;
  }

  for (i = 0; i < divs.length && i < plimit; i++) {
    src = divs[i].cloneNode(true);
    cnt.appendChild(src);
  }
  left = 0;
  top = e.offsetHeight + 1;
  parent = e;
  do {
    left += parent.offsetLeft;
    top += parent.offsetTop;
  } while ((parent = parent.offsetParent));
  if ((d = document.body.offsetWidth - left - width) < 0) left += d;
  cnt.setAttribute("style", "left:" + left + "px;top:" + top + "px;");
  document.body.appendChild(cnt);
}
function remove_quote_preview(e) {
  var cnt;
  if ((cnt = document.getElementById("q-p"))) document.body.removeChild(cnt);
}
/*function goTo(e) {
  e.preventDefault();
  var pst = this.textContent.split(/[-,]/)[0];
  pst = "r"+ pst.slice(2);
  pst = document.getElementById(pst);
  if (pst) pst.scrollIntoView(); 
}*/
function quotePreview() {
  if (localStorage.getItem("shobon_on") == "false") {
    return;
  }
  if (
    localStorage.getItem("shobon_preview") == "false" &&
    localStorage.getItem("shobon_backlink") == "false"
  ) {
    return;
  }

  var i, q, replies, quotes;

  if (document.body.className && document.body.className != "res")
    is_bbs = true;
  else is_bbs = false;

  if (is_bbs) replies = document.getElementsByClassName("msg");
  else replies = document.getElementsByTagName("blockquote");

  urls = window.location.pathname.split("/");
  cur_url = urls[0] + "/" + urls[1] + "/" + urls[2] + "/" + urls[3];

  for (x = 0; x < replies.length; x++) {
    quotes = replies[x].getElementsByTagName("a");
    linklist = {};

    for (i = 0; i < quotes.length; i++) {
      q = quotes[i];
      if (q.textContent.length < 3 || !q.textContent.startsWith(">>")) continue;

      if (localStorage.getItem("shobon_preview") != "false") {
        q.addEventListener("mouseover", who_are_you_quoting, false);
        q.addEventListener("mouseout", remove_quote_preview, false);
      }

      if (localStorage.getItem("shobon_backlink") != "false") {
        fill_links(q);
      }
    }
  }
  /*  if (document.body.className === "threadpage") {
    for (x = 0; x < replies.length; x++) {
      var q = replies[x].getElementsByTagName("a");
      for(var j=0;j<q.length;j++) {
        if(q[j].textContent.startsWith(">>")) q[j].addEventListener("click", goTo, false);
      }
    }
  }*/
}
document.addEventListener("DOMContentLoaded", quotePreview, false);