!function () {
	//监听来自页面内的脚本(injected-script)的消息
	window.addEventListener('message', function(e)
	{
		// console.log(e.data);
		switch (e.data.type) {
			case 'setBackgroundCacheValue':
				chrome.runtime.sendMessage({
					type: 'setBackgroundCacheValue',
					key: e.data.key,
					value: e.data.value
				});
				break;
			case 'delBackgroundCacheValue':
				chrome.runtime.sendMessage({
					type: 'delBackgroundCacheValue',
					key: e.data.key,
				});
				break;
			case 'getAllStorageOptions':
				chrome.runtime.sendMessage({
					type: 'getAllStorageOptions'
				}, function (options) {
					var tempElem = document.createElement('script');
					tempElem.setAttribute('type', 'text/javascript');
					tempElem.setAttribute('id', 'getAllStorageOptions');
					tempElem.innerHTML = 'window[\'ymOptions\'] = JSON.parse(\'{0}\');document.getElementById(\'getAllStorageOptions\').remove();'.format(JSON.stringify(options));
					document.head.appendChild(tempElem);
				});
				break;
			case 'setStorageOptions':
				chrome.runtime.sendMessage({
					type: 'setStorageOptions',
					key: e.data.key,
					value: e.data.value,
				});
				break;
			case 'executeScript':
				chrome.runtime.sendMessage({
					type: 'executeScript',
					details: e.data.details
				});
				break;
			case 'closeTab':
				chrome.runtime.sendMessage({
					type: 'closeTab',
					url: e.data.url,
					active: e.data.active
				});
				break;
			case 'parsePddItems':
				chrome.runtime.sendMessage({
					type: 'setBackgroundCacheValue',
					key: e.data.key,
					value: e.data.value
				});
				chrome.runtime.sendMessage({
					type: 'executeScript',
					files: ['server/js/pdd/itemrenderext.js']
				});
				break;
			case 'openCollectPage':
				chrome.runtime.sendMessage({
					type: "openTab",
					url: chrome.runtime.getURL('collect.html'),
					active: true
				});
				break;
		}
	}, false);

	// //监听来自background的消息
	// chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
	// {
	// 	switch (request.type) {
	// 		case 'executeScript':
	// 			var tempElem = document.createElement('script');
	// 			tempElem.setAttribute('type', 'text/javascript');
	// 			tempElem.src = request.url;
	// 			document.head.appendChild(tempElem);
	// 			break;
	// 	}
	//
	// });

	function insertCommonJsCss() {
		chrome.runtime.sendMessage({
			type: 'insertCSS',
			files: ['layui/css/layui.css', 'layui/css/modules/layer/default/layer.css', 'server/css/style.css']
		});
		if (window.location.href.indexOf('yangkeduo') < 0 && window.location.href.indexOf('shop.jd.com') < 0 && window.location.href.indexOf('shujymei.com') < 0 && window.location.href.indexOf('jd.com') < 0) {
			chrome.runtime.sendMessage({
				type: 'insertCSS',
				files: ['server/css/app.css']
			});
		}
		chrome.runtime.sendMessage({
			type: 'executeScript',
			files: ['js/lib/jquery.min.js', 'server/js/config.js', 'server/js/platformlogininfo.js', 'server/js/utils.js', 'layui/layui.js', 'layui/lay/modules/layer.js']
		});
	}

	function insertOperationJs() {
		// insertCommonJsCss();
		var pageMatchInfo = {
			'taobao': {
				'item': {
					'regExp': /taobao.*(item.htm)/g,
					'jsPaths': ['/server/js/taobao/ajaxhook.js','server/js/itemcollect.js'],
					'timeout': 0
				},
				'search': {
					'regExp': /taobao.*(search)|taobao.*(category)/g,
					'jsPaths': ['server/js/itemcollect.js', 'server/js/itemcollectactionbar.js'],
					'timeout': 0
				},
				'index': {
					'regExp': /https:\/\/www.(taobao).com/g,
					'jsPaths': [],
					'timeout': 0
				}
			},
			'tmall': {
				'item': {
					'regExp': /tmall.*(item.htm)/g,
					'jsPaths': ['server/js/itemcollect.js', 'js/lib/FileSaver.js', 'js/lib/jszip.min.js', '/server/js/taobao/ajaxhook.js'],
					'timeout': 0
				},
				'search': {
					'regExp': /tmall.*(search)|tmall.*(category)/g,
					'jsPaths': ['server/js/itemcollect.js', 'server/js/itemcollectactionbar.js'],
					'timeout': 0
				},
				'index': {
					'regExp': /https:\/\/www.(tmall).com/g,
					'jsPaths': [],
					'timeout': 0
				}
			},
			'ali1688': {
				'item': {
					'regExp': /detail.1688.*(offer)/g,
					'jsPaths': ['server/js/itemcollect.js'],
					'timeout': 0
				},
				'search': {
					'regExp': /1688.*(offer_search)|1688.*(offerlist.*?htm)/g,
					'jsPaths': ['server/js/itemcollect.js', 'server/js/itemcollectactionbar.js'],
					'timeout': 0
				},
				'index': {
					'regExp': /https:\/\/www.(1688).com/g,
					'jsPaths': [],
					'timeout': 0
				}
			},
			'jd': {
				'item': {
					'regExp': /item.*jd.*(htm)/g,
					'jsPaths': ['server/js/itemcollect.js', '/server/js/jd/ajaxhook.js'],
					'timeout': 0
				},
				'search': {
					'regExp': /search.jd.*(Search)|jd.com.*(view_search)/g,
					'jsPaths': ['server/js/itemcollect.js', 'server/js/itemcollectactionbar.js'],
					'timeout': 0
				},
				'index': {
					'regExp': /https:\/\/www.(jd).com/g,
					'jsPaths': [],
					'timeout': 0
				}
			},
			'pdd': {
				'item': {
					'regExp': /mobile.yangkeduo.*(goods.htm)/g,
					'jsPaths': ['server/js/itemcollect.js'],
					'timeout': 0
				},
				'search': {
					'regExp': /mobile.yangkeduo.*(search_result.htm)|mobile.yangkeduo.*(mall_page.htm)/g,
					'jsPaths': ['server/js/itemcollect.js', 'server/js/itemcollectactionbar.js', 'server/js/pdd/searchitemext.js'],
					'timeout': 0
				},
				'index': {
					'regExp': /https:\/\/www.(mobile.yangkeduo).com/g,
					'jsPaths': [],
					'timeout': 0
				}
			},
			'douyin': {
				'item': {
					'regExp': /haohuo.jinritemai.com.*(index.html)/g,
					'jsPaths': ['server/js/itemcollect.js', '/server/js/douyin/ajaxhook.js'],
					'timeout': 0
				}
			},
			'zhaojiafang': {
				'item': {
					'regExp': /detail.zhaojiafang.com\/goods\/(\d+)/g,
					'jsPaths': ['server/js/itemcollect.js'],
					'timeout': 0
				}
			},
			'vvic': {
				'item': {
					'regExp': /vvic.com\/item\/(.*)/g,
					'jsPaths': ['server/js/itemcollect.js'],
					'timeout': 0
				}
			},
			'server': {
				'jdShop': {
					'regExp': /http.*(shop.jd.com)/g,
					'jsPaths': [],
					'timeout': 0
				},
				'jdYm': {
					'regExp': /http.*(ym.shujymei.com)/g,
					'jsPaths': [],
					'timeout': 0
				},
				'jdPrint': {
					'regExp': /http.*(jp.shujymei.com)/g,
					'jsPaths': [],
					'timeout': 0
				},
				'xhsYm': {
					'regExp': /http.*(ym.xhs.shujymei.com)/g,
					'jsPaths': [],
					'timeout': 0
				},
				'dyYm': {
					'regExp': /http.*(ymdy.shujymei.com)/g,
					'jsPaths': [],
					'timeout': 0
				}
			}
		};

		var insertJsCssFlag = false;	//防止多次注入
		$.each(pageMatchInfo, function (platform, matchInfo) {
			// console.log(platform, matchInfo);
			$.each(matchInfo, function (page, item) {
				var ret = item['regExp'].exec(document.location.href);
				// console.log(item['regExp']);
				// console.log(ret);
				if (!insertJsCssFlag && ret && (ret[1] || ret[2])){
					var timeout = item.hasOwnProperty('timeout') ? item['timeout'] : 3000;
					insertCommonJsCss();
					setTimeout(function () {
						item['jsPaths'].length && chrome.runtime.sendMessage({
							type: 'executeScript',
							files: item['jsPaths'],
						});
					}, timeout);
					insertJsCssFlag = true;
				}
			});
		});
	}
	if (window.location.href.indexOf('/login') == -1) {	//登录页面不注入
		insertOperationJs();
	}
}();
