aboutsummaryrefslogtreecommitdiff
path: root/app/src/web/assets/js/context_a.js
blob: 61192b283da092aa77a6e10f30eee85dead81599 (plain)
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
"use strict";
(function () {
    var longClick = false;
    var _frostCopyComment = function (e, target) {
        if (!target.hasAttribute('data-commentid')) {
            return false;
        }
        var text = target.innerText;
        console.log("Copy comment " + text);
        Frost.contextMenu(null, text);
        return true;
    };
    var _frostCopyPost = function (e, target) {
        if (target.tagName !== 'A') {
            return false;
        }
        var parent1 = target.parentElement;
        if (!parent1 || parent1.tagName !== 'DIV') {
            return false;
        }
        var parent2 = parent1.parentElement;
        if (!parent2 || !parent2.classList.contains('story_body_container')) {
            return false;
        }
        var url = target.getAttribute('href');
        var text = parent1.innerText;
        console.log("Copy post " + url + " " + text);
        Frost.contextMenu(url, text);
        return true;
    };
    var _getImageStyleUrl = function (el) {
        var img = el.querySelector("[style*=\"background-image: url(\"]");
        if (!img) {
            return null;
        }
        return window.getComputedStyle(img, null).backgroundImage.trim().slice(4, -1);
    };
    var _frostImage = function (e, target) {
        var element = target;
        for (var i = 0; i < 2; i++) {
            if (element.tagName !== 'A') {
                element = element.parentElement;
            }
            else {
                break;
            }
        }
        if (element.tagName !== 'A') {
            return false;
        }
        var url = element.getAttribute('href');
        if (!url || url === '#') {
            return false;
        }
        var text = element.parentElement.innerText;
        var imageUrl = _getImageStyleUrl(element) || _getImageStyleUrl(element.parentElement);
        if (imageUrl) {
            console.log("Context image: " + imageUrl);
            Frost.loadImage(imageUrl, text);
            return true;
        }
        var img = element.querySelector("img[src*=scontent]");
        if (img instanceof HTMLMediaElement) {
            var imgUrl = img.src;
            console.log("Context img: " + imgUrl);
            Frost.loadImage(imgUrl, text);
            return true;
        }
        console.log("Context content " + url + " " + text);
        Frost.contextMenu(url, text);
        return true;
    };
    var handlers = [_frostImage, _frostCopyComment, _frostCopyPost];
    var _frostAContext = function (e) {
        Frost.longClick(true);
        longClick = true;
        var target = e.target || e.currentTarget || e.srcElement;
        if (!(target instanceof HTMLElement)) {
            console.log("No element found");
            return;
        }
        for (var _i = 0, handlers_1 = handlers; _i < handlers_1.length; _i++) {
            var h = handlers_1[_i];
            if (h(e, target)) {
                e.stopPropagation();
                e.preventDefault();
                return;
            }
        }
    };
    document.addEventListener('contextmenu', _frostAContext, true);
    document.addEventListener('touchend', function () {
        if (longClick) {
            Frost.longClick(false);
            longClick = false;
        }
    }, true);
}).call(undefined);