aboutsummaryrefslogtreecommitdiff
path: root/js/stub.js
blob: 59b2da9b7d0684ad44968dfdd1cbe7180926b1d8 (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
//Dummy file to use while developing the UI. This way we can just develop it on a local fileserver, and don't have to reload
//an extension for every tiny change!

if (!chrome || !chrome.storage || !chrome.storage.local) {

     let testData = {
        "createdBy": "Redirector v3.2",
        "createdAt": "2019-12-09T12:54:13.391Z",
        "redirects": [
            {
                "description": "Mbl test",
                "exampleUrl": "https://mbl.is",
                "exampleResult": "http://foo.is",
                "error": null,
                "includePattern": "*mbl*",
                "excludePattern": "",
                "patternDesc": "My description",
                "redirectUrl": "http://foo.is",
                "patternType": "R",
                "processMatches": "noProcessing",
                "disabled": false,
                "appliesTo": [
                    "main_frame",
                    "script"
                ]
            },
            {
                "description": "Msdfsdfbl test",
                "exampleUrl": "https://mbssfdsl.is",
                "exampleResult": "http://foo.is",
                "error": null,
                "includePattern": "*mbl*",
                "excludePattern": "",
                "patternDesc": "My description",
                "redirectUrl": "http://foo.is",
                "patternType": "W",
                "processMatches": "urlEncode",
                "disabled": false,
                "appliesTo": [
                    "main_frame",
                    "sub_frame"
                ]
            },            {
                "description": "https://foo.is?s=joh",
                "exampleUrl": "https://foo.is?s=joh",
                "exampleResult": "https://foo.is",
                "error": null,
                "includePattern": "(.*)(\\?s=)(.*)",
                "excludePattern": "",
                "patternDesc": "Test error",
                "redirectUrl": "$1",
                "patternType": "R",
                "processMatches": "noProcessing",
                "disabled": false,
                "appliesTo": [
                    "main_frame"
                ]
            }
        ]
    };

    localStorage.redirector = JSON.stringify(testData);


    //Make dummy for testing...
    window.chrome = window.chrome || {};
    chrome.storage = {
        local : {
            get : function(defaults, callback) {
                let data = JSON.parse(localStorage.redirector || '{}');
                
                let result = {};
                for (let key in defaults) {
                    if (typeof data[key] !== 'undefined') {
                        result[key] = data[key];
                    } else {
                        result[key] = defaults[key];
                    }
                }
                callback(result);
            },

            set : function(obj) {
                let data = JSON.parse(localStorage.redirector || '{}');
                
                for (let k in obj) {
                    data[k] = obj[k];
                }
                localStorage.redirector = JSON.stringify(data);
            }
        }
    };

    chrome.runtime = {
        sendMessage : function(params, callback) {
            let data = JSON.parse(localStorage.redirector || '{}');
            if (params.type === 'get-redirects') {
                chrome.storage.local.get({redirects:[]}, callback);
            } else if (params.type === 'toggle-sync') {
                if (params.isSyncEnabled) {
                    callback({message:'sync-enabled'});
                } else {
                    callback({message:'sync-disabled'});
                }
            }
        },
        getManifest : function() {
            return { version: '0-dev' };
        }
    };
}