post list.

kakao adfit

2018년 1월 16일 화요일

javascript 북마클릿 영어사전

네이버 영어사전 북마클릿을 잘 쓰고 있었는데 ​
네이버에서 서버 정책을 바꾼 것 같다.. 갑자기 다른 도메인에서 iFrame이 안떠짐 ㅠㅠ
그래서 구글사전을 쓸까했는데 역시나 안됨..

그나마 되는게 다음이라 좀 커스터마이즈를 해봤다.
그래봤자 jQuery의 animate함수 쓰려고 구글링에서 다이나믹 jQuery로딩 복붙한게 다지만..

평소엔 옆에 숨어있다가 모르는 단어 드래그하거나 더블클릭하면 쇽 나와서 알려주고
블럭해제하면 다시 삭 들어간다.

코드가 쓸데 없이 길어서 ㅠㅠ IE에서는 안되는 것 같다.
모두 크롬을 애용합시다. 구글 만세!

웹영어사전

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
javascript:(
function() {
    /*JQUERY LOAD DYNAMICALLY*/
    var done = false;
/*JQUERY VERSION*/
    var v = "1.3.2";
    /*IF PAGE HAS NO JQUERY OR JQUERY VERSION LOW*/
    if(window.jQuery == undefined || window.jQuery.fn.jQuery < v) {
        var script = document.createElement("script");
        script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
        script.onload = script.onReadyStateChange =
            function() {
                if(    !done    &&
                        (    !this.readyState                            ||
                            this.readyState == "loaded"        ||
                            this.readyState == "complete"        )
                ) {
                    console.log("jQuery load ok");
                    done = true;
                    initBookmarklet();
                }
            }; /*END OF DEFINITION OF SCRIPT ONLREADY FUNCTION*/
        document.getElementsByTagName("head")[0].appendChild(script);
    } else { /*END OF JQUERY VERSION CHECK*/
        initBookmarklet();
    }
        
    function initBookmarklet() {
            console.log("initBookmarklet()");
            if (document.getElementById('endicIframe')) {
                return;
            }
            
            var iframe = document.createElement('iframe');
            var closeBtn = document.createElement('button');
            var endicUrl = 
                /*DAUM SMALL*/
                'http://small.dic.daum.net/search.do?q=';
                /*GOOGLE SERVER BLOCKED*//*'https://translate.google.com/?hl=ko#en/ko/';*/
                /*NAVER SERVER BLOCKED*//*'http://endic.naver.com/popManager.nhn?m=search';*/
            var hideRightPx = "-380px";
            var showRightPx = "0px";
            
            iframe.id = 'endicIframe';
            iframe.style.position = 'fixed';
            iframe.style.top = 0;
            iframe.style.right = hideRightPx;
            iframe.style.zIndex = 10000;
            iframe.style.border = '1px solid #ddd';
            iframe.style.background = 'white';
            
            iframe.style.width = '400px';
            iframe.style.height = '400px';
            
            iframe.src = endicUrl;
            closeBtn.id = 'endicCloseBtn';
            closeBtn.style.position = 'fixed';
            closeBtn.style.top = iframe.style.top;
            closeBtn.style.right = iframe.style.right;
            closeBtn.style.zIndex = 11000;
            closeBtn.style.border = iframe.style.border;
            closeBtn.style.background = iframe.style.background;
            closeBtn.type = 'button';            
            closeBtn.style.padding = '7px 10px';
            closeBtn.innerHTML = 'close';
            
            var dblclickHandler =
                function() {
                    console.log("dblclickHandler");
                    var query = document.getSelection().toString();
                    if (query) {
                        iframe.src = endicUrl + query;
                        $("#endicIframe").animate({right:showRightPx});
                        $("#endicCloseBtn").animate({right:showRightPx});
                    } else {
                        $("#endicIframe").animate({right:hideRightPx});
                        $("#endicCloseBtn").animate({right:hideRightPx});
                    }
                };
            var mouseupHandler =
                function() {
                    console.log("mouseup");
                    var query = document.getSelection().toString();
                    if (query) {
                        iframe.src = endicUrl + query;
                        $("#endicIframe").animate({right:showRightPx});
                        $("#endicCloseBtn").animate({right:showRightPx});
                    } else {
                        $("#endicIframe").animate({right:hideRightPx});
                        $("#endicCloseBtn").animate({right:hideRightPx});
                    }
                };
            
            closeBtn.onclick =
                function () {
                    document.body.removeChild(iframe);
                    document.body.removeChild(closeBtn);
                    document.removeEventListener('dblclick', dblclickHandler, false);
                    document.removeEventListener('mouseup', mouseupHandler, false);
                };
            
            document.body.appendChild(iframe);
            document.body.appendChild(closeBtn);
            document.addEventListener('dblclick', dblclickHandler, false);
            document.addEventListener('mouseup', mouseupHandler, false);
    }    /*END OF FUNCTION initBookmarklet*/
        
})();    /*END OF BOOKMARKLET*/

댓글 없음 :

댓글 쓰기