<commit_before>/*
* @Author: dmyang
* @Date:   2015-11-10 10:21:22
* @Last Modified by:   dmyang
* @Last Modified time: 2016-06-01 10:56:50
* @Description: Hybrid core code
*/

'use strict'

const WIN = window
const UA = navigator.userAgent
const SLICE = Array.prototype.slice
const IS_ARR = Array.isArray
const EC = WIN.encodeURIComponent

const OS_NAME = 'HybridApp' // 推荐替换成公司标识，如淘宝“TaobaoCN”
const API_ROOT = 'HybridJS'
const SCHEMA = 'hybrid' // 私有协议，建议替换成公司私有协议，如腾讯“tencent”
const WEB_CB_SN_PREFIX = '__t_'
const REQ_SN_KEY = 'req_sn'
const RES_SN_KEY = 'res_sn'

const CALLBACKS = {}
const HANDLERS = {}

const Hybrid = WIN[API_ROOT] = {}
// ${OS_NAME}/o2o/1
let m = UA.match(new RegExp(OS_NAME + '\\/([^\\/]+)\\/(\\d+)'))

freeze('version', '1.0.0')
freeze('desc', `JavaScript layer for ${OS_NAME} hybrid framework.`)
freeze('deviceName', () => {
    // Mozilla/5.0 (Linux Android 5.1 MX5 Build/LMY471) AppleWebKit/600.1.3
    // return matchArray(UA, ['m1', 'm2', 'm note', 'm note2', 'm metal', 'MX3', 'MX4', 'MX4 Pro', 'MX5', 'PRO5', 'Unknown'])
    let m = UA.match(/\s*([^]+)\s+Build/)
    return m ? m[1] : 'Unknown'
})
freeze('os', matchArray(UA, [OS_NAME, 'Unknown']))
freeze('isInApp', Hybrid.os == OS_NAME)
freeze('appName', m ? m[1] : '')
freeze('appVersion', m ? m[2] : 0)

Hybrid.debug = 0
// 所有web提供给Native的接口都放到Hybrid.web对象上，避免冲突
Hybrid.web = {}

freeze('wrapAPI', () => wrapAPI)
freeze('assign', () => assignAPI)
freeze('invokeApp', () => invokeAppApi)
freeze('invokeWeb', () => invokeWebApi)
freeze('notifyWeb', () => appNotifyWeb)
freeze('on', () => on)
freeze('off', () => off)
// freeze('emit', () => emit)
freeze('util', () => {return { serialize, resolveUrl, parseUrl }})

/**
 * 把调用Native接口（即Hybrid.invokeApp）封装成js命名空间的方式
 * @param  {String}   api     api name
 * @param  {Function} wrapper 处理函数
 * @usage:
 * wrapAPI('device.getStatus', function(key, callback) {
 *     Hybrid.invokeApp('status.get', {key, callback}) // => hybrid://status/get?key=xx&req_sn=xxxxxx
 * })
 * @call:
 * Hybrid.device.getStatus('xx', function(result) {})
 */
function wrapAPI(api, wrapper) {
    if(/^web\./.test(api)) throw new Error('此处不要使用`web`作为根命名空间')
    inject(api, wrapper)
}

/**
 * 开放web api供app调用
 * @param  {String}   api      api name
 * @param  {Function} handler  处理函数
 * @usage:
 * assignAPI('web.dom.getNodeText', function(options) {return $(options.id).text()})
 */
function assignAPI(api, handler) {
    // if(/^app\./.test(api)) throw new Error('此处不要使用`app`作为根命名空间')
    inject('web', api, handler)
}

function inject(ns, api, handler) {
    if(arguments.length < 3) {<commit_msg>fix iframe<commit_after>/*
* @Author: dmyang
* @Date:   2015-11-10 10:21:22
* @Last Modified by:   dmyang
* @Last Modified time: 2016-07-28 20:59:27
* @Description: Hybrid core code
*/

'use strict'

const WIN = window
const UA = navigator.userAgent
const SLICE = Array.prototype.slice
const IS_ARR = Array.isArray
const EC = WIN.encodeURIComponent
// 判断是否在iframe里边加载此js
const IS_IN_IFRAME = WIN.top !== WIN.self

const OS_NAME = 'HybridApp' // 推荐替换成公司标识，如淘宝“TaobaoCN”
const API_ROOT = 'HybridJS'
const SCHEMA = 'hybrid' // 私有协议，建议替换成公司私有协议，如腾讯“tencent”
const WEB_CB_SN_PREFIX = '__t_'
const REQ_SN_KEY = 'req_sn'
const RES_SN_KEY = 'res_sn'

const CALLBACKS = {}
const HANDLERS = {}

const Hybrid = WIN[API_ROOT] = {}
// ${OS_NAME}/o2o/1
let m = UA.match(new RegExp(OS_NAME + '\\/([^\\/]+)\\/(\\d+)'))

freeze('version', '1.0.0')
freeze('desc', `JavaScript layer for ${OS_NAME} hybrid framework.`)
freeze('deviceName', () => {
    // Mozilla/5.0 (Linux Android 5.1 MX5 Build/LMY471) AppleWebKit/600.1.3
    // return matchArray(UA, ['m1', 'm2', 'm note', 'm note2', 'm metal', 'MX3', 'MX4', 'MX4 Pro', 'MX5', 'PRO5', 'Unknown'])
    let m = UA.match(/\s*([^]+)\s+Build/)
    return m ? m[1] : 'Unknown'
})
freeze('os', matchArray(UA, [OS_NAME, 'Unknown']))
freeze('isInApp', Hybrid.os == OS_NAME)
freeze('appName', m ? m[1] : '')
freeze('appVersion', m ? m[2] : 0)

Hybrid.debug = 0
// 所有web提供给Native的接口都放到Hybrid.web对象上，避免冲突
Hybrid.web = {}

freeze('wrapAPI', () => wrapAPI)
freeze('assign', () => assignAPI)
freeze('invokeApp', () => invokeAppApi)
freeze('invokeWeb', () => invokeWebApi)
freeze('notifyWeb', () => appNotifyWeb)
freeze('on', () => on)
freeze('off', () => off)
// freeze('emit', () => emit)
freeze('util', () => {return { serialize, resolveUrl, parseUrl }})

// 这里只处理了iframe一层嵌套的情况
if(IS_IN_IFRAME) {
    console.info(`${API_ROOT} running in iframe.`)
    const parent = WIN.parent
    const iframes = parent.document.getElementsByTagName('iframe')
    const selfHref = WIN.self.location.href
    let selfWin
    for(let i=0; i<iframes.length; i++)
        if(iframes[i].contentWindow.location.href === selfHref) selfWin = iframes[i].contentWindow
    // WIN.parent[API_ROOT] = Hybrid
    WIN.parent[API_ROOT] = {
        invokeWeb: selfWin[API_ROOT].invokeWeb
    }
}

/**
 * 把调用Native接口（即Hybrid.invokeApp）封装成js命名空间的方式
 * @param  {String}   api     api name
 * @param  {Function} wrapper 处理函数
 * @usage:
 * wrapAPI('device.getStatus', function(key, callback) {
 *     Hybrid.invokeApp('status.get', {key, callback}) // => hybrid://status/get?key=xx&req_sn=xxxxxx
 * })
 * @call:
 * Hybrid.device.getStatus('xx', function(result) {})
 */
function wrapAPI(api, wrapper) {
    if(/^web\./.test(api)) throw new Error('此处不要使用`web`作为根命名空间')
    inject(api, wrapper)
}

/**
 * 开放web api供app调用
 * @param  {String}   api      api name
 * @param  {Function} handler  处理函数
 * @usage:
 * assignAPI('web.dom.getNodeText', function(options) {return $(options.id).text()})
 */
function assignAPI(api, handler) {
    // if(/^app\./.test(api)) throw new Error('此处不要使用`app`作为根命名空间')
    inject('web', api, handler)
}

function inject(ns, api, handler) {
    if(arguments.length < 3) {
{'fraction_non_alphanumeric': 0.19651483781918563, 'fraction_numerical': 0.02087646652864044, 'mean_word_length': 4.231949458483754, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7406416', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2425, 'n_words': 458}