programing tip

크롬의 커스텀 프로토콜 핸들러

itbloger 2020. 10. 12. 07:18
반응형

크롬의 커스텀 프로토콜 핸들러


크롬에서 사용자 정의 프로토콜 핸들러를 어떻게 설정합니까? 다음과 같은 것 :

myprotocol : // testfile

http://example.com?query=testfile에 요청을 보낸 다음 내 확장에 httpresponse를 보내려면 이것이 필요합니다.


다음 메서드는 응용 프로그램을 URI Scheme에 등록합니다. 따라서 HTML 코드에서 mycustproto :사용 하여 로컬 애플리케이션을 트리거 할 수 있습니다 . Google Chrome 버전 51.0.2704.79m (64 비트)에서 작동합니다.

주로 인쇄 대화 상자가 나타나지 않고 문서를 자동으로 인쇄하는 데이 방법을 사용했습니다. 결과는 꽤 좋고 외부 애플리케이션을 브라우저와 통합하는 완벽한 솔루션입니다.

HTML 코드 (단순) :

<a href="mycustproto:Hello World">Click Me</a>

HTML 코드 (대안) :

<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});

URI Scheme은 다음과 같습니다.

레지스트리에서 URI Scheme을 수동으로 생성하거나 "mycustproto.reg"파일을 실행할 수 있습니다 (아래 참조).

HKEY_CURRENT_USER\Software\Classes
   mycustproto
      (Default) = "URL:MyCustProto Protocol"
      URL Protocol = ""
      DefaultIcon
         (Default) = "myprogram.exe,1"
      shell
         open
            command
               (Default) = "C:\Program Files\MyProgram\myprogram.exe" "%1"

mycustproto.reg 예 :

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\mycustproto]
"URL Protocol"="\"\""
@="\"URL:MyCustProto Protocol\""

[HKEY_CURRENT_USER\Software\Classes\mycustproto\DefaultIcon]
@="\"mycustproto.exe,1\""

[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell]

[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open]

[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open\command]
@="\"C:\\Program Files\\MyProgram\\myprogram.exe\" \"%1\""

C # 콘솔 애플리케이션-myprogram.exe :

using System;
using System.Collections.Generic;
using System.Text;

namespace myprogram
{
  class Program
  {
    static string ProcessInput(string s)
    {
       // TODO Verify and validate the input 
       // string as appropriate for your application.
       return s;
    }

    static void Main(string[] args)
    {
      Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);
      Console.WriteLine("\n\nArguments:\n");

      foreach (string s in args)
      {
        Console.WriteLine("\t" + ProcessInput(s));
      }

      Console.WriteLine("\nPress any key to continue...");
      Console.ReadKey();
    }
  }
}

먼저 프로그램을 실행하여 프로그램이 올바른 경로에 있는지 확인하십시오.

cmd> "C:\Program Files\MyProgram\myprogram.exe" "mycustproto:Hello World"

HTML 페이지에서 링크를 클릭하십시오.

처음으로 경고 창이 나타납니다.

enter image description here

Chrome에서 외부 프로토콜 핸들러 설정을 재설정하려면 :

Chrome에서 사용자 정의 프로토콜을 수락 한 적이 있고 설정을 재설정하려면 다음을 수행하십시오 (현재 Chrome에는 설정을 변경할 수있는 UI가 없습니다).

이 경로에서 " Local State "이 파일을 편집하십시오 .

C:\Users\Username\AppData\Local\Google\Chrome\User Data\

또는 다음으로 이동하십시오.

%USERPROFILE%\AppData\Local\Google\Chrome\User Data\

그런 다음 다음 문자열을 검색하십시오. protocol_handler

거기에서 사용자 지정 프로토콜을 볼 수 있습니다.

Note: Please close your Google Chrome before editing the file. Otherwise, the change you have made will be overwritten by Chrome.

Reference:

https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx


Chrome 13 now supports the navigator.registerProtocolHandler API. For example,

navigator.registerProtocolHandler(
    'web+custom', 'http://example.com/rph?q=%s', 'My App');

Note that your protocol name has to start with web+, with a few exceptions for common ones (like mailto, etc). For more details, see: http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler


This question is old now, but there's been a recent update to Chrome (at least where packaged apps are concerned)...

http://developer.chrome.com/apps/manifest/url_handlers

and

https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/url-handler

It allows you to register a handler for a URL (as long as you own it). Sadly no myprotocol:// but at least you can do http://myprotocol.mysite.com and can create a webpage there that points people to the app in the app store.


This is how I did it. Your app would need to install a few reg keys on installation, then in any browser you can just link to foo:\anythingHere.txt and it will open your app and pass it that value.

This is not my code, just something I found on the web when searching the same question. Just change all "foo" in the text below to the protocol name you want and change the path to your exe as well.

(put this in to a text file as save as foo.reg on your desktop, then double click it to install the keys) -----Below this line goes into the .reg file (NOT including this line)------

REGEDIT4

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

[HKEY_CLASSES_ROOT\foo\shell]

[HKEY_CLASSES_ROOT\foo\shell\open]

[HKEY_CLASSES_ROOT\foo\shell\open\command]
@="\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\"" 

open

C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\Default

open Preferences then search for excluded_schemes you will find it in 'protocol_handler' delete this excluded scheme(s) to reset chrome to open url with default application

참고URL : https://stackoverflow.com/questions/7087728/custom-protocol-handler-in-chrome

반응형