在chrome网页上通过点击链接或执行js打开本地文件夹

用来能在网页上方便的打开常用文件夹或文件。

方法:

下面文件放到c:/tools/位置;

执行mycall.reg添加注册信息;

试试在chrome里点击<a href=”mycall://openfolder?arg=d:”>D:</a>打开D盘;

继续完善mycall.py支持其他功能。

文件:

(1)mycall.reg

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\mycall]
@="URL:mycall Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\mycall\DefaultIcon]
@="cmd.exe,1"

[HKEY_CLASSES_ROOT\mycall\shell]

[HKEY_CLASSES_ROOT\mycall\shell\open]

[HKEY_CLASSES_ROOT\mycall\shell\open\command]
@="cmd /c c:/tools/mycall.bat \"%1\""

(2)mycall.bat

echo %1
python c:\tools\mycall.py %1

(3)mycall.py

# -*- coding:utf-8 -*-
import sys
import urllib.parse
import re
import os

# ^([^:/]+)://([^\?]+)(?:\?([^\?#]+))?(?:#([^#]+))?$
def parseUrl(url):
	matchObj = re.match( r'^([^:/]+)://([^\?]+)(?:\?([^\?#]+))?(?:#([^#]+))?$', url)
	if matchObj:
		protocol = matchObj.group(1)
		path = matchObj.group(2)
		query = matchObj.group(3)
		hash = matchObj.group(4)
		params = {}
		if query:
			arr = re.split("&", query)
			for keqv in arr:
				if keqv == "" or len(keqv) == 0:
					continue
				idx = keqv.find('=')
				if idx != -1:
					k = keqv[:idx]
					v = keqv[idx+1:]
				else:
					k = keqv
					v = ''
				if k in params:
					vv = params[k]
					if isinstance(vv, dict):
						vv.append(v)
					else:
						vv = [vv, v]
						params[k] = vv
				else:
					params[k] = v
		return {"protocol": protocol, "path": path, "query": query, "hash": hash, "params": params}
	else:
		return None
	
def callByUrl(callUrl):
	callUrl = urllib.parse.unquote(callUrl)
	print("===> ", 'url =', callUrl)
	
	urlInfo = parseUrl(callUrl)
	
	print("===> ", 'urlInfo =', urlInfo)
	
	callExe = urlInfo['path']
	callArgs = urlInfo['params'].get('arg')
	callOut = urlInfo['params'].get('out')

	if callArgs:
		if isinstance(callArgs, dict):
			if len(callArgs) == 0:
				callArgs = ['']
		else:
			callArgs = [callArgs]
	else:
		callArgs = ['']
	
	print("===> ", callExe, callArgs, callOut)


	exe = None
	if 'echo/' == callExe:
		exe = 'echo'
	elif 'openfolder/' == callExe:
		exe = 'explorer'
	elif 'opentext/' == callExe:
		exe = 'D:\\Program Files\\EmEditor\\EmEditor.exe'
	else:
		print("===> ", 'not found cmd');
		return
	
	args = ""
	for a in callArgs:
		if a == None or a == '':
			continue
		args = args + '"' + a + '"'

	cmd = '"' + exe + '" ' + args
	print("===> ", cmd)

	p = os.popen(cmd)
	lines = p.read()
	print(lines)

###########################################################
if __name__ == '__main__':
	print('参数个数为:', len(sys.argv), '个参数。')
	print('参数列表:', str(sys.argv))

	# exec: mycall://echo/?arg=123&out=d:/tmp/test.txt
	callUrl = sys.argv[1]
	callByUrl(callUrl)

 

    原文作者:superzlc
    原文地址: https://blog.csdn.net/superzlc/article/details/107602066
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞