@xiee/utils 1.1.7 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ // lazyload Disqus on my personal site
2
+ (function(d) {
3
+ var inIFrame = function() {
4
+ var iframe = true;
5
+ try { iframe = window.self !== window.top; } catch (e) {}
6
+ return iframe;
7
+ };
8
+ if (inIFrame()) return;
9
+ var t = d.getElementById('disqus_thread'), s = t.nextElementSibling,
10
+ a = d.querySelector('section.comments'),
11
+ rads = a.querySelectorAll('input[type="radio"]');
12
+ s.setAttribute('data-timestamp', +new Date());
13
+ var inputValues = function() {
14
+ var v = {};
15
+ rads.forEach(function(el) { v[el.id] = el.checked; });
16
+ return v;
17
+ }, selectTab2 = function() {
18
+ a.querySelector('#tab-2').checked = true;
19
+ return 'add';
20
+ }, showFullComment = function(el) {
21
+ if (el.name !== 'layout') return;
22
+ a.classList[el.id === 'layout-2' ? selectTab2() : 'remove']('comment-full');
23
+ }, inputRestore = function() {
24
+ var v = localStorage.getItem('comment-prefs');
25
+ if (!v) return;
26
+ v = JSON.parse(v);
27
+ rads.forEach(function(el) {
28
+ if (v[el.id]) {
29
+ el.checked = true;
30
+ showFullComment(el);
31
+ }
32
+ });
33
+ }, inputSave = function() {
34
+ showFullComment(this);
35
+ loadScript();
36
+ var v = JSON.stringify(inputValues());
37
+ try { localStorage.setItem('comment-prefs', v); } catch (e) {}
38
+ }, loadScript = function() {
39
+ var full = a.classList.contains('comment-full');
40
+ a.querySelectorAll('input[name="tabs"]').forEach(function(el, i) {
41
+ if (!full && !el.checked) return;
42
+ var p = a.querySelector('.panel:nth-of-type(' + (i + 1) + ') > script[data-src]');
43
+ if (!p || !p.dataset.src) return;
44
+ p.src = p.dataset.src;
45
+ p.removeAttribute('data-src');
46
+ });
47
+ };
48
+ // restore radio button values when opening page
49
+ inputRestore();
50
+ // save when radio button values change
51
+ rads.forEach(function(el) { el.addEventListener('change', inputSave); });
52
+ var b = false, l = function(scroll) {
53
+ if (b) return;
54
+ a.querySelector('.tabs').style.display = '';
55
+ loadScript();
56
+ b = true;
57
+ if (scroll) t.scrollIntoView();
58
+ }
59
+ s.onerror = function(e) {
60
+ // try to load Disqus in the read-only mode
61
+ var s2 = d.createElement('script');
62
+ s2.src = "//cdn.jsdelivr.net/gh/yihui/DisqusJS@1.3.3/src/disqus.min.js";
63
+ t.appendChild(s2);
64
+ try { localStorage.setItem('dsqjs_mode', 'dsqjs'); } catch (e) {}
65
+ s2.onload = function(e) {
66
+ var s3 = d.createElement('link');
67
+ s3.rel = 'stylesheet';
68
+ s3.href = '//cdn.jsdelivr.net/gh/yihui/DisqusJS@1.3.3/src/disqusjs.min.css';
69
+ d.head.appendChild(s3);
70
+ disqus_config.DisqusJSInit();
71
+ };
72
+ s2.onerror = function(e) {
73
+ t.innerText = 'Sorry, but you cannot make comments because Disqus failed to load for some reason. It is known to be blocked in certain regions. If you are sure it is not blocked in your region, please refresh the page. Alternatively, you can comment with Utterances. 您可能需要翻墙发表 Disqus 评论;若没有梯子,不妨尝试使用 Github 登录 Utterances 评论。';
74
+ t.style.border = '1px dashed';
75
+ t.style.padding = '.5em';
76
+ t.style.background = 'lightyellow';
77
+ selectTab2();
78
+ };
79
+ };
80
+ // show comments when the hash means to jump to a comment
81
+ if (location.hash.match(/^#comment-[0-9]+$/)) return l(true);
82
+ var c = function() {
83
+ if (b) return;
84
+ var rect = a.getBoundingClientRect();
85
+ if (rect.top < window.innerHeight && rect.bottom >= 0) l();
86
+ };
87
+ window.addEventListener('load', c);
88
+ d.addEventListener('scroll', c);
89
+ })(document);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiee/utils",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Miscellaneous tools and utilities to manipulate HTML pages",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"