summarylogtreecommitdiffstats
path: root/fix-menu.sh
blob: 9adcfdd9221453e01626846c44b46d4f01a4098a (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
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
#! /bin/bash
# Author: https://github.com/cytle/wechat_web_devtools/issues/293#issuecomment-604623774

set -e

root_dir=$(cd `dirname $0`/.. && pwd -P)
NW_PACKAGE_DIR="$root_dir/package.nw"

cd "$NW_PACKAGE_DIR"
target_file=js/unpack/hackrequire/index.js

if [ ! -f "$target_file" ]; then
    echo -e "\e[1;31m$target_file is not exist\e[0m" >&2
    exit 1
fi
# 判断匹配函数,匹配函数不为0,则包含给定字符
if [ `grep -c "patch wechat devtools begin" $target_file` -ne '0' ];then
    echo -e "\e[1;31m$target_file seems to have been modified\e[0m" >&2
    exit 1
fi

tmp_file=$(mktemp)
cat > "$tmp_file" <<EOF
/* patch wechat devtools begin */
/* nw-menu.js */
(() => {
    try {
        if (typeof nw === "undefined") {
            return;
        }
        
        let log = function (content) {
            process.stderr.write(content + "\n");
        };
        
        let originMenuItem = nw.MenuItem;
        nw.MenuItem = function MenuItem(options) {
        
            options = Object.assign({}, options);
        
            delete options.shortcutName;
            delete options.shouldEnabled;
        
            if (options.label && (typeof options.label === "string")) {
        
                if (options.label.indexOf("[") !== -1) {
                    let rest = options.label.split("[").slice(1).join("[").trim();
                    if (rest[rest.length - 1] === "]") {
                        rest = rest.slice(0, -1).split("+").map((x) => {
                            if (!x) { return "+" }
                            switch (x) {
                                case "↓": { return "Down"; }
                                case "↑": { return "Up"; }
                                case "PAGE↓": { return "PageDown"; }
                                case "PAGE↑": { return "PageUp"; }
                                case "←": { return "Left"; }
                                case "→": { return "Right"; } 
                                default: { return x; }
                            }
                        });
                        if (rest.length > 1) {
                            options.key = rest[rest.length - 1];
                            options.modifiers = rest.slice(0, -1).join("+");
                        } else {
                            options.key = rest[0];
                        }
                    }
                    options.label = options.label.split("[")[0];
                }
        
                if (options.label.indexOf("(&") !== -1) {
                    options.label = options.label.split("(&")[0];
                }
                options.label = options.label.replace("&", "").trim();
        
                switch (options.label) {
                    case "Go to Declaration": { options.label = "转到声明"; break; }
                    case "Go to References": { options.label = "转到引用"; break; }
                    case "Find All References": { options.label = "查找所有引用"; break; }
                    case "Find All Implementations": { options.label = "查找所有实现"; break; }
                }
        
            }
        
            return new originMenuItem(options);
        
        };
        
        let originAppend = nw.Menu.prototype.append;
        nw.Menu.prototype.append = function (item) {
        
            if (item.parentMenu) {
                item.parentMenu.remove(item);
            }
            item.parentMenu = this;
            
            if ((this.items.length > 0) && 
                (this.items[this.items.length - 1].type === "separator") &&
                (item.type === "separator")) {
                originInsert.call(this, item, this.items.length);
                return;
            }
        
            if ((this.items.length === 0) && (item.type === "separator")) {
                originInsert.call(this, item, this.items.length);
                return;
            }
        
            return originAppend.call(this, item);
        };
        
        let originInsert = nw.Menu.prototype.insert;
        nw.Menu.prototype.insert = function (item, index) {
            if (item.parentMenu) {
                item.parentMenu.remove(item);
            }
            item.parentMenu = this;
            return originInsert.call(this, item, index);
        };
    } catch (error) {
        process.stderr.write(error.message);
        process.stderr.write(error.stack);
    }
})();
/* patch wechat devtools end */
EOF
cat "$target_file" >> "$tmp_file"

cat "$tmp_file" > "$target_file"
rm "$tmp_file"