├── .gitignore ├── test ├── mocha.opts ├── chai.conf.js ├── index.html └── event.spec.js ├── README.md ├── bower.json ├── webpack.config.js ├── LICENSE.md ├── package.json ├── example └── index.html ├── karma.conf.js ├── dist ├── kiwoom-helper.min.js └── kiwoom-helper.js └── src └── kiwoom-helper.js /.gitignore: -------------------------------------------------------------------------------- 1 | coverage 2 | .DS_Store 3 | node_modules 4 | *.map 5 | coverage 6 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers js:babel-register 2 | --reporter spec 3 | --harmony 4 | --ui bdd 5 | -------------------------------------------------------------------------------- /test/chai.conf.js: -------------------------------------------------------------------------------- 1 | var expect; 2 | if(typeof require === "function") { 3 | expect = require('chai').expect; 4 | require('chai').should(); 5 | } else { 6 | expect = chai.expect; 7 | chai.should(); 8 | } 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kiwoom-Helper 2 | QWebview-Plus에서 제공하는 kiwoom 객체와 이벤트를 손쉽게 사용할 수 있는 유틸 3 | 4 | ## Development Environment 5 | - [QWebview-Plus](https://github.com/sculove/QWebview-plus) 6 | 7 | ## Install 8 | ```sh 9 | bower install Kiwoom-Helper 10 | npm install Kiwoom-Helper 11 | ``` 12 | 13 | ## License 14 | Licensed under MIT: 15 | https://opensource.org/licenses/MIT 16 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Kiwoom-Helper", 3 | "description": "supports simple Kiwoom API for QWebview-Plus", 4 | "main": "dist/kiwoom-helper.min.js", 5 | "authors": [ 6 | "sculove" 7 | ], 8 | "license": "MIT", 9 | "keywords": [ 10 | "kiwoom" 11 | ], 12 | "homepage": "https://github.com/sculove/Kiwoom-Helper", 13 | "ignore": [ 14 | "**/.*", 15 | "node_modules", 16 | "bower_components", 17 | "test", 18 | "tests" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Helper Tests 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

Index

14 |
15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var NODE_ENV = process.env.NODE_ENV || 'development'; 2 | var webpack = require("webpack"); 3 | var config = { 4 | context: __dirname + '/src', 5 | entry : { 6 | build : "./kiwoom-helper.js" 7 | }, 8 | output: { 9 | filename : NODE_ENV === "production" ? "kiwoom-helper.min.js" : "kiwoom-helper.js", 10 | path: __dirname + "/dist" 11 | }, 12 | // devtool: NODE_ENV === "production" ? "source-map" : "inline-source-map", 13 | module: { 14 | loaders: [ 15 | { 16 | test: /\.js$/, 17 | loader: "babel-loader", 18 | exclude: /node_modules/, 19 | query: { 20 | presets: [ 21 | "es2015-loose" 22 | ] 23 | } 24 | } 25 | ] 26 | }, 27 | plugins: [ 28 | ] 29 | }; 30 | 31 | if (NODE_ENV === 'production') { 32 | config.plugins.push( 33 | new webpack.optimize.UglifyJsPlugin({ 34 | compress: { 35 | warnings: false, 36 | drop_console: true 37 | }, 38 | }) 39 | ); 40 | } 41 | module.exports = config; -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Kiwoom-Helper 2 | 3 | Copyright (c) 2016 sculove. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Kiwoom-Helper", 3 | "version": "1.0.5", 4 | "description": "QWebview-Plus에서 제공하는 kiwoom 객체와 이벤트를 손쉽게 사용할 수 있는 유틸", 5 | "main": "dist/kiwoom-helper.js", 6 | "scripts": { 7 | "start": "set NODE_ENV=development && export NODE_ENV=development && webpack -d -w --colors", 8 | "build": "set NODE_ENV=production && export NODE_ENV=production && webpack -p --colors", 9 | "test": "./node_modules/.bin/karma start" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git@github.com:sculove/Kiwoom-Helper.git" 14 | }, 15 | "author": "sculove", 16 | "license": "MIT", 17 | "bugs": { 18 | "url": "https://github.com/sculove/Kiwoom-Helper/issues" 19 | }, 20 | "keywords": [ 21 | "kiwoom", 22 | "stock", 23 | "QWebview" 24 | ], 25 | "homepage": "https://github.com/sculove/Kiwoom-Helper", 26 | "devDependencies": { 27 | "babel-core": "^6.6.4", 28 | "babel-loader": "^6.2.4", 29 | "babel-polyfill": "^6.7.4", 30 | "babel-preset-es2015": "^6.6.0", 31 | "babel-preset-es2015-loose": "^7.0.0", 32 | "chai": "^3.5.0", 33 | "karma": "^0.13.22", 34 | "karma-babel-preprocessor": "^6.0.1", 35 | "karma-chrome-launcher": "^1.0.1", 36 | "karma-coverage": "^1.0.0", 37 | "karma-firefox-launcher": "^1.0.0", 38 | "karma-ie-launcher": "^1.0.0", 39 | "karma-mocha": "^1.0.1", 40 | "karma-mocha-reporter": "^2.0.3", 41 | "karma-phantomjs-launcher": "^1.0.0", 42 | "karma-safari-launcher": "^1.0.0", 43 | "karma-webpack": "^1.7.0", 44 | "mocha": "^2.4.5", 45 | "phantomjs-prebuilt": "^2.1.7", 46 | "jquery": "^3.1.0", 47 | "sinon": "^1.17.4" 48 | }, 49 | "dependencies": { 50 | "es6-promise": "^3.2.1", 51 | "webpack": "^1.13.1" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Kiwoom-Helper Example 6 | 7 | 8 |
9 | 10 |
11 | 12 | 13 | 14 | 15 | 61 | 62 | -------------------------------------------------------------------------------- /test/event.spec.js: -------------------------------------------------------------------------------- 1 | describe("Event", () => { 2 | "use strict"; 3 | beforeEach( () => { 4 | window.kiwoom = window.kiwoom || { 5 | getRepeatCnt : () => { return 0; } 6 | }; 7 | }); 8 | describe("on", () => { 9 | context("sync events", () => { 10 | it("should call handler (single)", () => { 11 | //Given 12 | var handler_opt10001 = sinon.spy(); 13 | 14 | //When 15 | KiwoomHelper.on("receiveTrData", "opt10001", handler_opt10001); 16 | KiwoomHelper.handleEvent({ 17 | detail: { 18 | trCode: "opt10001" 19 | }, 20 | type: "receiveTrData.kiwoom" 21 | }); 22 | 23 | //Then 24 | handler_opt10001.calledOnce.should.be.true; 25 | handler_opt10001.args[0][0]["trCode"].should.equal("opt10001"); 26 | handler_opt10001.args[0][0]["size"].should.be.a("number"); 27 | Object.keys(KiwoomHelper._eventHandler).should.have.lengthOf(1); 28 | }); 29 | 30 | it("should call handlers (multi)", () => { 31 | //Given 32 | var handler_opt10001 = sinon.spy(); 33 | var handler_opt10081 = sinon.spy(); 34 | 35 | //When 36 | KiwoomHelper.on("receiveTrData", { 37 | "opt10001": handler_opt10001, 38 | "opt10081": handler_opt10081 39 | }); 40 | KiwoomHelper.handleEvent({ 41 | detail: { 42 | trCode: "opt10001" 43 | }, 44 | type: "receiveTrData.kiwoom" 45 | }); 46 | KiwoomHelper.handleEvent({ 47 | detail: { 48 | trCode: "opt10081" 49 | }, 50 | type: "receiveTrData.kiwoom" 51 | }); 52 | 53 | //Then 54 | handler_opt10001.calledOnce.should.be.true; 55 | handler_opt10001.args[0][0]["trCode"].should.equal("opt10001"); 56 | handler_opt10001.args[0][0]["size"].should.be.a("number"); 57 | handler_opt10081.calledOnce.should.be.true; 58 | handler_opt10081.args[0][0]["trCode"].should.equal("opt10081"); 59 | handler_opt10081.args[0][0]["size"].should.be.a("number"); 60 | Object.keys(KiwoomHelper._eventHandler).should.have.lengthOf(2); 61 | }); 62 | 63 | afterEach( () => { 64 | KiwoomHelper.off(); 65 | }); 66 | }); 67 | }); 68 | }); -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration 2 | // Generated on Fri May 13 2016 14:46:47 GMT+0900 (KST) 3 | var webpackConfig = require('./webpack.config.js'); 4 | webpackConfig.entry = {}; 5 | 6 | module.exports = function(config) { 7 | config.set({ 8 | 9 | // base path that will be used to resolve all patterns (eg. files, exclude) 10 | basePath: '', 11 | 12 | 13 | // frameworks to use 14 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 15 | frameworks: ['mocha'], 16 | 17 | 18 | // list of files / patterns to load in the browser 19 | files: [ 20 | // dependencies 21 | './node_modules/babel-polyfill/dist/polyfill.js', 22 | './node_modules/jquery/dist/jquery.min.js', 23 | 24 | // test dependencies 25 | './node_modules/chai/chai.js', 26 | './test/chai.conf.js', 27 | './node_modules/sinon/pkg/sinon.js', 28 | 29 | // src 30 | './src/*.js', 31 | 32 | // tests 33 | './test/*.spec.js' 34 | ], 35 | 36 | 37 | // list of files to exclude 38 | exclude: [ 39 | ], 40 | 41 | 42 | // preprocess matching files before serving them to the browser 43 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 44 | preprocessors: { 45 | 'src/*.js': ['babel', 'coverage'], 46 | 'test/*.spec.js': ['babel', 'coverage'] 47 | }, 48 | babelPreprocessor: { 49 | options: { 50 | presets: ['es2015'], 51 | plugins: ['transform-es2015-modules-umd'] 52 | } 53 | }, 54 | coverageReporter: { 55 | type : 'html', 56 | dir : 'coverage/' 57 | }, 58 | 59 | // test results reporter to use 60 | // possible values: 'dots', 'progress' 61 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter 62 | reporters: ['mocha', 'coverage'], 63 | mochaReporter: { 64 | output: 'autowatch' 65 | }, 66 | 67 | // web server port 68 | port: 9976, 69 | 70 | 71 | // enable / disable colors in the output (reporters and logs) 72 | colors: true, 73 | 74 | 75 | // level of logging 76 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 77 | logLevel: config.LOG_INFO, 78 | 79 | 80 | // enable / disable watching file and executing tests whenever any file changes 81 | autoWatch: false, 82 | 83 | 84 | // start these browsers 85 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 86 | browsers: ['PhantomJS'],//, 'Chrome', 'Firefox', 'Safari'], 87 | 88 | 89 | // Continuous Integration mode 90 | // if true, Karma captures browsers, runs the tests and exits 91 | singleRun: true, 92 | 93 | // Concurrency level 94 | // how many browser should be started simultaneous 95 | concurrency: Infinity, 96 | 97 | webpack: webpackConfig 98 | }); 99 | }; 100 | -------------------------------------------------------------------------------- /dist/kiwoom-helper.min.js: -------------------------------------------------------------------------------- 1 | !function(e){function t(o){if(n[o])return n[o].exports;var i=n[o]={exports:{},id:o,loaded:!1};return e[o].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){return e+"_"+t}t.__esModule=!0;var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},r=function(){function e(){n(this,e),window.KiwoomHelper=this,this._eventHandler={},this._screenNo=0,this._requestScreens=[],this._registerScreens=[],this._attach()}return e.prototype.isQWebviewPlus=function(){return"kiwoom"in window},e.prototype._attach=function(){var t=this;e.EVENTS.forEach(function(e){document.addEventListener(e+".kiwoom",t)})},e.prototype._getScreenNo=function(){return"SRC"+this._screenNo++},e.prototype.handleEvent=function(e){var t=e.detail,n=e.type.substring(0,e.type.indexOf(".kiwoom"));"receiveTrData"===n&&(t.size=kiwoom?kiwoom.getRepeatCnt(t.trCode,t.rQName):0);var i=this._eventHandler[o(n,t.trCode)];i&&i.call(this,t)},e.prototype.on=function(t,n,r){var s=this;if(!this.isQWebviewPlus())return this;if(e.EVENTS.indexOf(t)!==-1)return"object"===("undefined"==typeof n?"undefined":i(n))&&"undefined"==typeof r?(Object.keys(n).forEach(function(e){s.on(t,e,n[e])}),this):("string"==typeof n&&"function"==typeof r&&(this._eventHandler[o(t,n)]=r),this)},e.prototype.off=function(e,t){var n=this;return this.isQWebviewPlus()?0===arguments.length?(this._eventHandler={},this):("undefined"==typeof t?this._eventHandler[e]=void 0:"string"==typeof t?this._eventHandler[o(e,t)]=void 0:Array.isArray(t)&&Object.keys(this._eventHandler).filter(function(t){return 0===t.indexOf(e+"_")}).forEach(function(t){n._eventHandler[o(e,t)]=void 0}),this):this},e.prototype.isLogin=function(){return!!this.isQWebviewPlus()&&kiwoom.getConnectState()},e.prototype.login=function(){return this.isQWebviewPlus()?new Promise(function(e,t){var n=kiwoom.getConnectState();if(0==n){var o=kiwoom.commConnect();o<0&&t(Error(o))}else 1==n&&e();var i=function r(n){var o=n.detail;0==o?e():t(Error(o)),document.removeEventListener("eventConnect.kiwoom",r)};document.addEventListener("eventConnect.kiwoom",i)}):Promise.reject()},e.prototype.getLoginInfo=function(){return this.isQWebviewPlus()?{account:kiwoom.getLoginInfo("ACCNO").replace(/;$/,"").split(";"),user:{id:kiwoom.getLoginInfo("USER_ID"),name:kiwoom.getLoginInfo("USER_NAME")}}:{}},e.prototype.request=function(e,t){var n=!(arguments.length<=2||void 0===arguments[2])&&arguments[2];if(!this.isQWebviewPlus())return-1;Object.keys(t).forEach(function(e){kiwoom.setInputValue(e,t[e])});var o=this._getScreenNo(),i=kiwoom.commRqData(e,e,n?2:0,o);return 0===i&&this._requestScreens.push(o),o},e.prototype.register=function(e,t){var n=arguments.length<=2||void 0===arguments[2]||arguments[2];if(!this.isQWebviewPlus())return-1;Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]);var o=this._getScreenNo(),i=kiwoom.setRealReg(o,e.join(";"),t.join(";"),n?0:1);return 0===i&&this._registerScreens.push(o),i},e.prototype.disconnect=function(e){var t=arguments.length<=1||void 0===arguments[1]?"ALL":arguments[1];this.isQWebviewPlus()&&(e?this._requestScreens.indexOf(e)!==-1?kiwwom.DisconnectRealData(e):this._registerScreens.indexOf(e)!==-1&&kiwwom.SetRealRemove(e,t):(this._requestScreens.forEach(function(e){kiwwom.DisconnectRealData(e)}),this._requestScreens.length=0,kiwwom.SetRealRemove("ALL","ALL"),this._registerScreens.length=0))},e.prototype.get=function(){if(!this.isQWebviewPlus())return null;switch(arguments.length){case 1:return JSON.parse(kiwoom.getCommDataEx(arguments[0],arguments[0]));case 2:return kiwoom.commGetData(arguments[0],"-1","",arguments[1],"");case 3:if(!isNaN(arguments[1])&&isNaN(arguments[2])){var e;return(e=kiwoom).commGetData.apply(e,[arguments[0],""].concat(Array.prototype.slice.call(arguments)))}var t;return(t=kiwoom).commGetData.apply(t,Array.prototype.slice.call(arguments).concat([0,""]))}},e}();r.EVENTS=["receiveMsg","receiveTrData","receiveRealData","receiveChejanData","receiveConditionVer","receiveTrCondition","receiveRealCondition"],t["default"]=new r}]); -------------------------------------------------------------------------------- /src/kiwoom-helper.js: -------------------------------------------------------------------------------- 1 | // import { Promise } from "es6-promise"; 2 | 3 | // 이벤트의 키를 얻음 4 | function getEventKey(eventName, trCode) { 5 | return eventName + "_" + trCode; 6 | } 7 | 8 | class KiwoomHelper { 9 | constructor() { 10 | window.KiwoomHelper = this; 11 | this._eventHandler = {}; 12 | this._screenNo = 0; 13 | this._requestScreens = []; 14 | this._registerScreens = []; 15 | this._attach(); 16 | } 17 | isQWebviewPlus() { 18 | return "kiwoom" in window; 19 | } 20 | _attach() { 21 | // 사용자 구분 요청 명은 trCode와 동일하다. 22 | KiwoomHelper.EVENTS.forEach(v => { 23 | document.addEventListener(v + ".kiwoom", this); 24 | }); 25 | } 26 | // 화면 번호를 얻음 27 | _getScreenNo() { 28 | return "SRC" + (this._screenNo++); 29 | } 30 | handleEvent(e) { 31 | // console.log(e.type, e); 32 | let data = e.detail; 33 | let type = e.type.substring(0, e.type.indexOf(".kiwoom")); 34 | if (type === "receiveTrData") { 35 | data.size = kiwoom ? kiwoom.getRepeatCnt(data.trCode, data.rQName) : 0; 36 | } 37 | 38 | let handler = this._eventHandler[getEventKey(type, data.trCode)]; 39 | // console.log(type,data,handler); 40 | handler && handler.call(this, data); 41 | } 42 | /** 43 | * Attach an event handler function. 44 | * @method KiwoomHelper#on 45 | * @param {String} eventName 46 | * @param {String} trCode 47 | * @param {Function} handler 48 | * @return {this} instance of itself 49 | * @example 50 | // 코드별 핸들러 등록 (string) 51 | KiwoomHelper.on("receiveTrData", "opt10001", function(data) { 52 | // ... 53 | }); 54 | // 코드별 핸들러 등록 (hashmap) 55 | KiwoomHelper.on("receiveTrData", { 56 | "opt10001" : function(data) { // ... }, 57 | "opt10002" : function(data) { // ... } 58 | }); 59 | */ 60 | on(eventName, trCode, handler) { 61 | if ( !this.isQWebviewPlus() ) { 62 | return this; 63 | } 64 | if ( KiwoomHelper.EVENTS.indexOf(eventName) === -1) { 65 | return; 66 | } 67 | 68 | // trCode가 object 인 경우. 69 | if (typeof trCode === "object" && typeof handler === "undefined") { 70 | Object.keys(trCode).forEach(v => { 71 | this.on(eventName, v, trCode[v]); 72 | }); 73 | return this; 74 | // 단건인 경우. 75 | } else if (typeof trCode === "string" && typeof handler === "function") { 76 | this._eventHandler[getEventKey(eventName, trCode)] = handler; 77 | } 78 | return this; 79 | } 80 | /** 81 | * Detach an event handler function. 82 | * @method KiwoomHelper#off 83 | * @param {String} eventName 84 | * @param {String} trCode 85 | * @return {this} instance of itself 86 | * @example 87 | KiwoomHelper.off(); 88 | KiwoomHelper.off("receiveTrData.kiwoom"); 89 | KiwoomHelper.off("receiveTrData.kiwoom", "opt10001"); 90 | KiwoomHelper.off("receiveTrData.kiwoom", ["opt10001", "opt10002"] ); 91 | */ 92 | off(eventName, trCode) { 93 | if ( !this.isQWebviewPlus() ) { 94 | return this; 95 | } 96 | // All event detach. 97 | if (arguments.length === 0) { 98 | this._eventHandler = {}; 99 | return this; 100 | } 101 | if (typeof trCode === "undefined" ) { 102 | this._eventHandler[eventName] = undefined; 103 | } else if (typeof trCode === "string") { 104 | this._eventHandler[getEventKey(eventName, trCode)] = undefined; 105 | } else if (Array.isArray(trCode)) { 106 | Object.keys(this._eventHandler) 107 | .filter(v => v.indexOf(eventName + "_") === 0) 108 | .forEach(v => { 109 | this._eventHandler[getEventKey(eventName, v)] = undefined; 110 | }); 111 | } 112 | return this; 113 | } 114 | /** 115 | * isLogin 116 | * @method KiwoomHelper#isLogin 117 | * @return {Number} login status 118 | * @example 119 | KiwoomHelper.isLogin(); 120 | */ 121 | isLogin() { 122 | if ( !this.isQWebviewPlus() ) { 123 | return false; 124 | } 125 | return kiwoom.getConnectState(); 126 | } 127 | /** 128 | * login 129 | * @method KiwoomHelper#login 130 | * @return {Promise} Promise instance 131 | * @example 132 | KiwoomHelper.login().then(function() { 133 | // ... 134 | }); 135 | */ 136 | login() { 137 | if ( !this.isQWebviewPlus() ) { 138 | return Promise.reject(); 139 | } 140 | return new Promise((resolve, reject) => { 141 | const status = kiwoom.getConnectState(); 142 | if(status == 0) { 143 | let rt = kiwoom.commConnect(); 144 | rt < 0 && reject(Error(rt)); 145 | } else if(status == 1) { 146 | resolve(); 147 | } 148 | let hander = (e) => { 149 | var errcode = e.detail; 150 | errcode == 0 ? resolve() : reject(Error(errcode)); 151 | document.removeEventListener("eventConnect.kiwoom", hander); 152 | } 153 | document.addEventListener("eventConnect.kiwoom", hander); 154 | 155 | }); 156 | } 157 | /** 158 | * get login information 159 | * @method KiwoomHelper#getLoginInfo 160 | * @return {Object} user information 161 | * @example 162 | KiwoomHelper.getLoginInfo(); 163 | */ 164 | getLoginInfo() { 165 | if ( !this.isQWebviewPlus() ) { 166 | return {}; 167 | } 168 | return { 169 | account : kiwoom.getLoginInfo("ACCNO").replace(/;$/,"").split(";"), 170 | user : { 171 | id : kiwoom.getLoginInfo("USER_ID"), 172 | name : kiwoom.getLoginInfo("USER_NAME") 173 | } 174 | }; 175 | } 176 | /** 177 | * request 178 | * @method KiwoomHelper#request 179 | * @param {String} trCode 180 | * @param {Object} input 181 | * @param {Boolean} isContinue=false 182 | * @return {String} screenNo 183 | * @example 184 | KiwoomHelper.request("opt10001", { 185 | "종목코드" : "035420" 186 | }); 187 | */ 188 | request(trCode, input, isContinue = false) { 189 | if ( !this.isQWebviewPlus() ) { 190 | return -1; 191 | } 192 | //@todo screenNo 200개가 넘으면 안된다. 193 | //실시간 조건검색은 최대 10개 194 | Object.keys(input).forEach(v => { 195 | kiwoom.setInputValue(v, input[v]); 196 | }); 197 | // 화면 번호는 자동으로 198 | let screenNo = this._getScreenNo(); 199 | let result = kiwoom.commRqData(trCode, trCode, isContinue ? 2 : 0, screenNo); 200 | if(result === 0) { 201 | // 화면 번호를 저장 202 | this._requestScreens.push(screenNo); 203 | } 204 | return screenNo; 205 | } 206 | register(stockcodes, fids, isNew = true) { 207 | if ( !this.isQWebviewPlus() ) { 208 | return -1; 209 | } 210 | if(!Array.isArray(stockcodes)) { 211 | stockcodes = [stockcodes]; 212 | } 213 | if(!Array.isArray(fids)) { 214 | fids = [fids]; 215 | } 216 | let screenNo = this._getScreenNo(); 217 | let result = kiwoom.setRealReg(screenNo, stockcodes.join(";"), fids.join(";"), isNew ? 0 : 1); 218 | 219 | if(result === 0) { 220 | // 화면 번호를 저장 221 | this._registerScreens.push(screenNo); 222 | } 223 | return result; 224 | } 225 | /** 226 | * disconnect 227 | * @method KiwoomHelper#disconnect 228 | * @param {String} screenNo 229 | * @param {String} [stockcode="ALL"] 230 | * @example 231 | KiwoomHelper.disconnect("SRC1"); 232 | KiwoomHelper.disconnect("SRC2", ""035420"); 233 | */ 234 | disconnect(screenNo, stockcode = "ALL") { 235 | if ( !this.isQWebviewPlus() ) { 236 | return; 237 | } 238 | if(screenNo) { 239 | if(this._requestScreens.indexOf(screenNo) !== -1) { 240 | kiwwom.DisconnectRealData(screenNo); 241 | } else if(this._registerScreens.indexOf(screenNo) !== -1) { 242 | kiwwom.SetRealRemove(screenNo, stockcode); 243 | } 244 | } else { 245 | this._requestScreens.forEach(v => { 246 | kiwwom.DisconnectRealData(v); 247 | }); 248 | this._requestScreens.length = 0; 249 | kiwwom.SetRealRemove("ALL", "ALL"); 250 | this._registerScreens.length = 0; 251 | } 252 | } 253 | get() { 254 | if ( !this.isQWebviewPlus() ) { 255 | return null; 256 | } 257 | switch(arguments.length) { 258 | case 1: 259 | // 대용량 데이터 (trCode만 입력) 260 | return JSON.parse(kiwoom.getCommDataEx(arguments[0], arguments[0])); 261 | case 2: 262 | // 체결 데이터 263 | return kiwoom.commGetData(arguments[0], "-1", "", arguments[1], ""); 264 | case 3: 265 | if(!isNaN(arguments[1]) && isNaN(arguments[2])) { 266 | // trans 데이터 267 | // get(“OPT00001”, 0, “현재가”); 268 | return kiwoom.commGetData(arguments[0], "", ...arguments); 269 | } else { 270 | // real 데이터 271 | // 종목코드, realType, fid 272 | // get("000600", 'A', 10); 273 | return kiwoom.commGetData(...arguments, 0, ""); 274 | } 275 | } 276 | } 277 | } 278 | KiwoomHelper.EVENTS = [ 279 | "receiveMsg", 280 | "receiveTrData", 281 | "receiveRealData", 282 | "receiveChejanData", 283 | "receiveConditionVer", 284 | "receiveTrCondition", 285 | "receiveRealCondition" 286 | ]; 287 | 288 | // singleton 289 | export default new KiwoomHelper(); -------------------------------------------------------------------------------- /dist/kiwoom-helper.js: -------------------------------------------------------------------------------- 1 | /******/ (function(modules) { // webpackBootstrap 2 | /******/ // The module cache 3 | /******/ var installedModules = {}; 4 | /******/ 5 | /******/ // The require function 6 | /******/ function __webpack_require__(moduleId) { 7 | /******/ 8 | /******/ // Check if module is in cache 9 | /******/ if(installedModules[moduleId]) 10 | /******/ return installedModules[moduleId].exports; 11 | /******/ 12 | /******/ // Create a new module (and put it into the cache) 13 | /******/ var module = installedModules[moduleId] = { 14 | /******/ exports: {}, 15 | /******/ id: moduleId, 16 | /******/ loaded: false 17 | /******/ }; 18 | /******/ 19 | /******/ // Execute the module function 20 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 21 | /******/ 22 | /******/ // Flag the module as loaded 23 | /******/ module.loaded = true; 24 | /******/ 25 | /******/ // Return the exports of the module 26 | /******/ return module.exports; 27 | /******/ } 28 | /******/ 29 | /******/ 30 | /******/ // expose the modules object (__webpack_modules__) 31 | /******/ __webpack_require__.m = modules; 32 | /******/ 33 | /******/ // expose the module cache 34 | /******/ __webpack_require__.c = installedModules; 35 | /******/ 36 | /******/ // __webpack_public_path__ 37 | /******/ __webpack_require__.p = ""; 38 | /******/ 39 | /******/ // Load entry module and return exports 40 | /******/ return __webpack_require__(0); 41 | /******/ }) 42 | /************************************************************************/ 43 | /******/ ([ 44 | /* 0 */ 45 | /*!**************************!*\ 46 | !*** ./kiwoom-helper.js ***! 47 | \**************************/ 48 | /***/ function(module, exports) { 49 | 50 | "use strict"; 51 | 52 | exports.__esModule = true; 53 | 54 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; 55 | 56 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 57 | 58 | // import { Promise } from "es6-promise"; 59 | 60 | // 이벤트의 키를 얻음 61 | function getEventKey(eventName, trCode) { 62 | return eventName + "_" + trCode; 63 | } 64 | 65 | var KiwoomHelper = function () { 66 | function KiwoomHelper() { 67 | _classCallCheck(this, KiwoomHelper); 68 | 69 | window.KiwoomHelper = this; 70 | this._eventHandler = {}; 71 | this._screenNo = 0; 72 | this._requestScreens = []; 73 | this._registerScreens = []; 74 | this._attach(); 75 | } 76 | 77 | KiwoomHelper.prototype.isQWebviewPlus = function isQWebviewPlus() { 78 | return "kiwoom" in window; 79 | }; 80 | 81 | KiwoomHelper.prototype._attach = function _attach() { 82 | var _this = this; 83 | 84 | // 사용자 구분 요청 명은 trCode와 동일하다. 85 | KiwoomHelper.EVENTS.forEach(function (v) { 86 | document.addEventListener(v + ".kiwoom", _this); 87 | }); 88 | }; 89 | // 화면 번호를 얻음 90 | 91 | 92 | KiwoomHelper.prototype._getScreenNo = function _getScreenNo() { 93 | return "SRC" + this._screenNo++; 94 | }; 95 | 96 | KiwoomHelper.prototype.handleEvent = function handleEvent(e) { 97 | // console.log(e.type, e); 98 | var data = e.detail; 99 | var type = e.type.substring(0, e.type.indexOf(".kiwoom")); 100 | if (type === "receiveTrData") { 101 | data.size = kiwoom ? kiwoom.getRepeatCnt(data.trCode, data.rQName) : 0; 102 | } 103 | 104 | var handler = this._eventHandler[getEventKey(type, data.trCode)]; 105 | // console.log(type,data,handler); 106 | handler && handler.call(this, data); 107 | }; 108 | /** 109 | * Attach an event handler function. 110 | * @method KiwoomHelper#on 111 | * @param {String} eventName 112 | * @param {String} trCode 113 | * @param {Function} handler 114 | * @return {this} instance of itself 115 | * @example 116 | // 코드별 핸들러 등록 (string) 117 | KiwoomHelper.on("receiveTrData", "opt10001", function(data) { 118 | // ... 119 | }); 120 | // 코드별 핸들러 등록 (hashmap) 121 | KiwoomHelper.on("receiveTrData", { 122 | "opt10001" : function(data) { // ... }, 123 | "opt10002" : function(data) { // ... } 124 | }); 125 | */ 126 | 127 | 128 | KiwoomHelper.prototype.on = function on(eventName, trCode, handler) { 129 | var _this2 = this; 130 | 131 | if (!this.isQWebviewPlus()) { 132 | return this; 133 | } 134 | if (KiwoomHelper.EVENTS.indexOf(eventName) === -1) { 135 | return; 136 | } 137 | 138 | // trCode가 object 인 경우. 139 | if ((typeof trCode === "undefined" ? "undefined" : _typeof(trCode)) === "object" && typeof handler === "undefined") { 140 | Object.keys(trCode).forEach(function (v) { 141 | _this2.on(eventName, v, trCode[v]); 142 | }); 143 | return this; 144 | // 단건인 경우. 145 | } else if (typeof trCode === "string" && typeof handler === "function") { 146 | this._eventHandler[getEventKey(eventName, trCode)] = handler; 147 | } 148 | return this; 149 | }; 150 | /** 151 | * Detach an event handler function. 152 | * @method KiwoomHelper#off 153 | * @param {String} eventName 154 | * @param {String} trCode 155 | * @return {this} instance of itself 156 | * @example 157 | KiwoomHelper.off(); 158 | KiwoomHelper.off("receiveTrData.kiwoom"); 159 | KiwoomHelper.off("receiveTrData.kiwoom", "opt10001"); 160 | KiwoomHelper.off("receiveTrData.kiwoom", ["opt10001", "opt10002"] ); 161 | */ 162 | 163 | 164 | KiwoomHelper.prototype.off = function off(eventName, trCode) { 165 | var _this3 = this; 166 | 167 | if (!this.isQWebviewPlus()) { 168 | return this; 169 | } 170 | // All event detach. 171 | if (arguments.length === 0) { 172 | this._eventHandler = {}; 173 | return this; 174 | } 175 | if (typeof trCode === "undefined") { 176 | this._eventHandler[eventName] = undefined; 177 | } else if (typeof trCode === "string") { 178 | this._eventHandler[getEventKey(eventName, trCode)] = undefined; 179 | } else if (Array.isArray(trCode)) { 180 | Object.keys(this._eventHandler).filter(function (v) { 181 | return v.indexOf(eventName + "_") === 0; 182 | }).forEach(function (v) { 183 | _this3._eventHandler[getEventKey(eventName, v)] = undefined; 184 | }); 185 | } 186 | return this; 187 | }; 188 | /** 189 | * isLogin 190 | * @method KiwoomHelper#isLogin 191 | * @return {Number} login status 192 | * @example 193 | KiwoomHelper.isLogin(); 194 | */ 195 | 196 | 197 | KiwoomHelper.prototype.isLogin = function isLogin() { 198 | if (!this.isQWebviewPlus()) { 199 | return false; 200 | } 201 | return kiwoom.getConnectState(); 202 | }; 203 | /** 204 | * login 205 | * @method KiwoomHelper#login 206 | * @return {Promise} Promise instance 207 | * @example 208 | KiwoomHelper.login().then(function() { 209 | // ... 210 | }); 211 | */ 212 | 213 | 214 | KiwoomHelper.prototype.login = function login() { 215 | if (!this.isQWebviewPlus()) { 216 | return Promise.reject(); 217 | } 218 | return new Promise(function (resolve, reject) { 219 | var status = kiwoom.getConnectState(); 220 | if (status == 0) { 221 | var rt = kiwoom.commConnect(); 222 | rt < 0 && reject(Error(rt)); 223 | } else if (status == 1) { 224 | resolve(); 225 | } 226 | var hander = function hander(e) { 227 | var errcode = e.detail; 228 | errcode == 0 ? resolve() : reject(Error(errcode)); 229 | document.removeEventListener("eventConnect.kiwoom", hander); 230 | }; 231 | document.addEventListener("eventConnect.kiwoom", hander); 232 | }); 233 | }; 234 | /** 235 | * get login information 236 | * @method KiwoomHelper#getLoginInfo 237 | * @return {Object} user information 238 | * @example 239 | KiwoomHelper.getLoginInfo(); 240 | */ 241 | 242 | 243 | KiwoomHelper.prototype.getLoginInfo = function getLoginInfo() { 244 | if (!this.isQWebviewPlus()) { 245 | return {}; 246 | } 247 | return { 248 | account: kiwoom.getLoginInfo("ACCNO").replace(/;$/, "").split(";"), 249 | user: { 250 | id: kiwoom.getLoginInfo("USER_ID"), 251 | name: kiwoom.getLoginInfo("USER_NAME") 252 | } 253 | }; 254 | }; 255 | /** 256 | * request 257 | * @method KiwoomHelper#request 258 | * @param {String} trCode 259 | * @param {Object} input 260 | * @param {Boolean} isContinue=false 261 | * @return {String} screenNo 262 | * @example 263 | KiwoomHelper.request("opt10001", { 264 | "종목코드" : "035420" 265 | }); 266 | */ 267 | 268 | 269 | KiwoomHelper.prototype.request = function request(trCode, input) { 270 | var isContinue = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2]; 271 | 272 | if (!this.isQWebviewPlus()) { 273 | return -1; 274 | } 275 | //@todo screenNo 200개가 넘으면 안된다. 276 | //실시간 조건검색은 최대 10개 277 | Object.keys(input).forEach(function (v) { 278 | kiwoom.setInputValue(v, input[v]); 279 | }); 280 | // 화면 번호는 자동으로 281 | var screenNo = this._getScreenNo(); 282 | var result = kiwoom.commRqData(trCode, trCode, isContinue ? 2 : 0, screenNo); 283 | if (result === 0) { 284 | // 화면 번호를 저장 285 | this._requestScreens.push(screenNo); 286 | } 287 | return screenNo; 288 | }; 289 | 290 | KiwoomHelper.prototype.register = function register(stockcodes, fids) { 291 | var isNew = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; 292 | 293 | if (!this.isQWebviewPlus()) { 294 | return -1; 295 | } 296 | if (!Array.isArray(stockcodes)) { 297 | stockcodes = [stockcodes]; 298 | } 299 | if (!Array.isArray(fids)) { 300 | fids = [fids]; 301 | } 302 | var screenNo = this._getScreenNo(); 303 | var result = kiwoom.setRealReg(screenNo, stockcodes.join(";"), fids.join(";"), isNew ? 0 : 1); 304 | 305 | if (result === 0) { 306 | // 화면 번호를 저장 307 | this._registerScreens.push(screenNo); 308 | } 309 | return result; 310 | }; 311 | /** 312 | * disconnect 313 | * @method KiwoomHelper#disconnect 314 | * @param {String} screenNo 315 | * @param {String} [stockcode="ALL"] 316 | * @example 317 | KiwoomHelper.disconnect("SRC1"); 318 | KiwoomHelper.disconnect("SRC2", ""035420"); 319 | */ 320 | 321 | 322 | KiwoomHelper.prototype.disconnect = function disconnect(screenNo) { 323 | var stockcode = arguments.length <= 1 || arguments[1] === undefined ? "ALL" : arguments[1]; 324 | 325 | if (!this.isQWebviewPlus()) { 326 | return; 327 | } 328 | if (screenNo) { 329 | if (this._requestScreens.indexOf(screenNo) !== -1) { 330 | kiwwom.DisconnectRealData(screenNo); 331 | } else if (this._registerScreens.indexOf(screenNo) !== -1) { 332 | kiwwom.SetRealRemove(screenNo, stockcode); 333 | } 334 | } else { 335 | this._requestScreens.forEach(function (v) { 336 | kiwwom.DisconnectRealData(v); 337 | }); 338 | this._requestScreens.length = 0; 339 | kiwwom.SetRealRemove("ALL", "ALL"); 340 | this._registerScreens.length = 0; 341 | } 342 | }; 343 | 344 | KiwoomHelper.prototype.get = function get() { 345 | if (!this.isQWebviewPlus()) { 346 | return null; 347 | } 348 | switch (arguments.length) { 349 | case 1: 350 | // 대용량 데이터 (trCode만 입력) 351 | return JSON.parse(kiwoom.getCommDataEx(arguments[0], arguments[0])); 352 | case 2: 353 | // 체결 데이터 354 | return kiwoom.commGetData(arguments[0], "-1", "", arguments[1], ""); 355 | case 3: 356 | if (!isNaN(arguments[1]) && isNaN(arguments[2])) { 357 | var _kiwoom; 358 | 359 | // trans 데이터 360 | // get(“OPT00001”, 0, “현재가”); 361 | return (_kiwoom = kiwoom).commGetData.apply(_kiwoom, [arguments[0], ""].concat(Array.prototype.slice.call(arguments))); 362 | } else { 363 | var _kiwoom2; 364 | 365 | // real 데이터 366 | // 종목코드, realType, fid 367 | // get("000600", 'A', 10); 368 | return (_kiwoom2 = kiwoom).commGetData.apply(_kiwoom2, Array.prototype.slice.call(arguments).concat([0, ""])); 369 | } 370 | } 371 | }; 372 | 373 | return KiwoomHelper; 374 | }(); 375 | 376 | KiwoomHelper.EVENTS = ["receiveMsg", "receiveTrData", "receiveRealData", "receiveChejanData", "receiveConditionVer", "receiveTrCondition", "receiveRealCondition"]; 377 | 378 | // singleton 379 | exports.default = new KiwoomHelper(); 380 | 381 | /***/ } 382 | /******/ ]); 383 | //# sourceMappingURL=kiwoom-helper.js.map --------------------------------------------------------------------------------