{"id":21981,"date":"2013-11-18T12:21:21","date_gmt":"2013-11-18T19:21:21","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/aspnet\/?p=21981"},"modified":"2013-11-18T12:21:21","modified_gmt":"2013-11-18T19:21:21","slug":"building-browser-link-extension-in-visual-studio-2013","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/building-browser-link-extension-in-visual-studio-2013\/","title":{"rendered":"Building Browser Link extension in Visual Studio 2013"},"content":{"rendered":"<p>Browser Link feature in Visual Studio 2013 provides APIs for you to write your own Browser Link Extension. To take a look at some of the extensions, you can <a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/56633663-6799-41d7-9df7-0f2a504ca361\">download Web Essentials<\/a> for Visual Studio 2013 RTM.<\/p>\n<p>In this blog, I will give you a step by step overview of the various APIs that we provide.<\/p>\n<p>You will need the following to start with:<\/p>\n<ol>\n<li>Visual Studio 2013 RTM.<\/li>\n<li>Visual Studio 2013 SDK.<\/li>\n<li>Go to <a href=\"http:\/\/sidewaffle.com\/\" title=\"http:\/\/sidewaffle.com\/\">http:\/\/sidewaffle.com\/<\/a> and <a href=\"http:\/\/sidewaffle.com\/feed\/web\/templatepack.vsix\">download<\/a> a template for extensions.<\/li>\n<\/ol>\n<p>Once you have installed all of the above, you can get started:<\/p>\n<p>**Step 1: ** Go to\u00a0 File \u2013&gt;New-&gt; Visual C# \u2013&gt; Extensibility and select Browser Link Extension as shown in the image below.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/2313.image_737D9E3F.png\"><img decoding=\"async\" title=\"image\" style=\"margin: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/5040.image_thumb_281DFA7B.png\" width=\"642\" height=\"442\" \/><\/a><\/p>\n<p>Click OK, to create MyBrowserLinkProject. The browser link project will have a C# file and a JavaScript file. In the sample project, we have MyBrowserLinkProjectExtension.cs file and a MyBrowserLinkProjectExtension.js file.<\/p>\n<p>MyBrowserLinkProjectExtension.js contains the script that will be injected in every browser that is invoked after Visual Studio is launched.<\/p>\n<p><strong>Step 2:<\/strong> Open file MyBrowserLinkProjectExtension.cs.<\/p>\n<p>Here you will find a MyExtensionFactory class that implements interface <em>IBrowserLinkExtensionFactory<\/em> and MyExtension class that derives from base class <em>BrowserLinkExtension<\/em>.<\/p>\n<p>MyExtensionFactory class has a <em>CreateExtensionInstance<\/em>() method and a <em>GetScript<\/em>().<\/p>\n<p>CreateExtensionInstance method takes in a connection object. A connection can be thought of as a link to a page being rendered. For example, if Visual Studio has IE and Chrome browser rendering a particular page, then each of these can be thought of as a unique connection. You can create a new instance of an extension object or return the same instance of the extension object depending on what you want to do per connection.<\/p>\n<p>\u00a0<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9e293167-13ad-4968-885e-02bfa8bc504e\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkExtension<\/span><span style=\"background:#ffffff;color:#000000\"> CreateExtensionInstance(<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkConnection<\/span><span style=\"background:#ffffff;color:#000000\"> connection)<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">MyExtension<\/span><span style=\"background:#ffffff;color:#000000\">();<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>GetScript\u00a0 method just reads the script to use from MyBrowserLinkProject.js file.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:50cd8c54-33fe-4265-b15c-be3855feecc9\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> GetScript()<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">using<\/span><span style=\"background:#ffffff;color:#000000\"> (<\/span><span style=\"background:#ffffff;color:#2b91af\">Stream<\/span><span style=\"background:#ffffff;color:#000000\"> stream = GetType().Assembly.GetManifestResourceStream(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;MyBrowserLinkProject.Scripts.MyBrowserLinkProject.js&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">))<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">using<\/span><span style=\"background:#ffffff;color:#000000\"> (<\/span><span style=\"background:#ffffff;color:#2b91af\">StreamReader<\/span><span style=\"background:#ffffff;color:#000000\"> reader = <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">StreamReader<\/span><span style=\"background:#ffffff;color:#000000\">(stream))<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\"> reader.ReadToEnd();<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>\u00a0<\/p>\n<p><strong>Step 3:<\/strong> Now, let us look at the MyExtension class itself. This file contains an OnConnected method which you can implement and any number of Browser Link Callback methods.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a439f8ab-d4ce-417d-8491-2b88039ba4b5\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">class<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">MyExtension<\/span><span style=\"background:#ffffff;color:#000000\"> : <\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkExtension<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">override<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> OnConnected(<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkConnection<\/span><span style=\"background:#ffffff;color:#000000\"> connection)<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">Browsers.Client(connection).Invoke(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;greeting&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">, <\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;Hello from Visual Studio!&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li>\n          \u00a0\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">[<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkCallback<\/span><span style=\"background:#ffffff;color:#000000\">] <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ This method can be called from JavaScript<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> SendText(<\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> message)<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">MessageBox<\/span><span style=\"background:#ffffff;color:#000000\">.Show(message);<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>\u00a0<\/p>\n<p>OnConnected() will be called when a connection between Visual studio and browser has happened. This is\u00a0 a good place to do any kind of initialization. In our example, we have chosen to Invoke the greeting function in Javascript.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1caf40ed-b012-445d-9eeb-aee47398c686\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">override<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> OnConnected(<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkConnection<\/span><span style=\"background:#ffffff;color:#000000\"> connection)<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">Browsers.Client(connection).Invoke(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;greeting&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">, <\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;Hello from Visual Studio!&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>\u00a0<\/p>\n<p>This is how we can make a call from Visual Studio to the browser. In the above example we are trying to call the greetings function in the browser from within visual studio\u2019s extension.<\/p>\n<p><strong>Step 4:<\/strong> Open MyBrowserLinkProjectExtension.js file and you will see the greeting function which you invoked in step 3.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3d114be4-3673-4696-978d-a48aefc8d86b\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#0000ff\">function<\/span><span style=\"background:#ffffff;color:#000000\"> (browserLink, $) {<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#008000\">\/\/\/ \n          <\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#008000\">\/\/\/ \n          <\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          \u00a0\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">function<\/span><span style=\"background:#ffffff;color:#000000\"> output(message) { <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ Helper for the &#8216;greeting&#8217; function<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">if<\/span><span style=\"background:#ffffff;color:#000000\"> (console) {<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">console.log(message);<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          \u00a0\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\"> {<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          \u00a0\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">greeting: <\/span><span style=\"background:#ffffff;color:#0000ff\">function<\/span><span style=\"background:#ffffff;color:#000000\"> (message) { <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ Can be called from the server-side extension<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">output(message);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">},<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          \u00a0\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">onConnected: <\/span><span style=\"background:#ffffff;color:#0000ff\">function<\/span><span style=\"background:#ffffff;color:#000000\"> () { <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ Optional. Is called when a connection is established<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">browserLink.invoke(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;SendText&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">, <\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;Hello from &#8220;<\/span><span style=\"background:#ffffff;color:#000000\"> + browserLink.initializationData.appName);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">};<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">});<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>Now let us see how we can make a call from Browser to Visual studio. For this, you will need Browser Link Callback methods defined in your extension.<\/p>\n<p>In our example, you can see that we have a \u201cSendText\u201d method with a [BrowserLinkCallback] attribute. This means, this method can be called from javascript running in your browser.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:487dc4c1-db2c-4e0a-b0ee-73940a5b6424\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#000000\"> [<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkCallback<\/span><span style=\"background:#ffffff;color:#000000\">] <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ This method can be called from JavaScript<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> SendText(<\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> message)<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">MessageBox<\/span><span style=\"background:#ffffff;color:#000000\">.Show(message);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>\u00a0<\/p>\n<p><strong>Step 5:<\/strong> Do F5 and this will open up the experimental instance of VS. Here, create a new webforms website. Bring up the Browser Link dashboard. Click on the view in browser link here.<\/p>\n<p>This will bring up a Message box which says \u201cHello from Internet Explorer\u201d. This way you can see Browser\u2019s JavaScript invoking a Visual studio method.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/4062.image_0C0D3B90.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/2818.image_thumb_6BF22ED2.png\" width=\"642\" height=\"293\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<p><strong>Step 6:<\/strong> If you invoke F12 tools in IE, you can see a Browserlink.js file that plugs in the extension script for you in all the browsers.<\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1832.image_79C474CD.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/2402.image_thumb_72A53855.png\" width=\"642\" height=\"427\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<p>You can also see that Visual studio has successfully printed in your console. This was done by greetings function in your JavaScript.<\/p>\n<p>Step 1 to 6 was a basic introduction of some of the APIs that you would require to get started.<\/p>\n<p>** Step 7:** Let us look at Actions API.<\/p>\n<p>These APIs provide a way for your Actions(BrowserLinkCallBack Methods) to be invoked via the Browser link Dashboard.<\/p>\n<p>This will give you an entry in the context menu of your connection right next to the Refresh Extension as shown in the image below.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/6644.image_528A2B98.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/5148.image_thumb_4760A14E.png\" width=\"242\" height=\"157\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<p>In our Example, we have two actions defined. One is \u201cAction for SendText\u201d and the other is \u201cAction for Greetings\u201d.<\/p>\n<p>To do this you will need to add the following piece of code:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bf3270e3-46aa-4798-92b9-345cd64b937e\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">override<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">IEnumerable<\/span><span style=\"background:#ffffff;color:#000000\">&lt; <\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkAction<\/span><span style=\"background:#ffffff;color:#000000\">&gt; Actions<\/span> <\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">get<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">return<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkAction<\/span><span style=\"background:#ffffff;color:#000000\">[] { <\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkAction<\/span><span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;Action for SendText&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">, <\/span><span style=\"background:#ffffff;color:#0000ff\">this<\/span><span style=\"background:#ffffff;color:#000000\">.AddSendText),<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkAction<\/span><span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;Action for SendGreetings&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">, <\/span><span style=\"background:#ffffff;color:#0000ff\">this<\/span><span style=\"background:#ffffff;color:#000000\">.AddGreetings),<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">};<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>Note that, each of these Action methods AddSendText and AddGreetings inturn invoke a BrowserLinkCallBack method.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f0cadac0-093d-4e8f-aa6f-c86a983c3cee\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> AddSendText(<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkAction<\/span><span style=\"background:#ffffff;color:#000000\"> action)<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">SendText(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;Invoked from Actions context menu!&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li>\n          \u00a0\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> AddGreetings(<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkAction<\/span><span style=\"background:#ffffff;color:#000000\"> action)<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">SendGreetings(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;Invoked via Actions: Hello from Visual Studio!&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>Here are their corresponding BrowserLinkCallBack methods:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:621964fc-23a6-4918-9e02-90f0bdc40e90\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#000000\">[<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkCallback<\/span><span style=\"background:#ffffff;color:#000000\">] <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ This method can be called from JavaScript<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> SendText(<\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> message)<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#2b91af\">MessageBox<\/span><span style=\"background:#ffffff;color:#000000\">.Show(message);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          \u00a0\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">[<\/span><span style=\"background:#ffffff;color:#2b91af\">BrowserLinkCallback<\/span><span style=\"background:#ffffff;color:#000000\">] <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ This method can be called from JavaScript<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> SendGreetings(<\/span><span style=\"background:#ffffff;color:#0000ff\">string<\/span><span style=\"background:#ffffff;color:#000000\"> message)<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">{<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">Browsers.Client(_browserlinkConnection).Invoke(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;greeting&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">, message);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>\u00a0<\/p>\n<p>At this point, you can do an F5 on MyBrowserLinkProject. This will open up the experimental instance of VS. Create a Website here and Browse About.aspx with IE and Chrome.<\/p>\n<p>Now click on the little arrow next to chrome and that will bring up the Actions menu for you.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/8371.image_795841D8.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/4162.image_thumb_2B4FE263.png\" width=\"234\" height=\"169\" \/><\/a><\/p>\n<p>Click on Action for SendGreetings and it will print out the following in Chrome\u2019s console as shown in the image below. This lets you test out the BrowserLinkCallback methods quickly on a per connection basis. You can also invoke this on IE and see similar results.<\/p>\n<p>\u00a0<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/2018.image_20265819.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/7536.image_thumb_000B4B5C.png\" width=\"644\" height=\"329\" \/><\/a><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<p><strong>Step 8:<\/strong> Mapping Data API.<\/p>\n<p>The API will provide two kinds of operations: mapping a source file position to a DOM element, and mapping a DOM element to a source file range.<\/p>\n<p>All mapping operations will involve a DOM element reference. DOM elements only exist in the browser; they cannot be marshaled to the Visual Studio process. Because of this, the API will only be available in JavaScript. There will be no way to use the API directly from C#.<\/p>\n<p>Each element that can be mapped will have a sourceMapping property attached to it.<\/p>\n<p>Let us see a quick example. Open file MyBrowserLinkProjectExtension.js.<\/p>\n<p>Inside the greeting method, add the following two lines of code. In the example, we are trying to get div1 and open the source file with the complete range for div tag selected.<\/p>\n<p>\u00a0<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:29a2a407-a727-4ff2-95ab-068c87b770c6\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">\n      Code Snippet\n    <\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li>\n          <span style=\"background:#ffffff;color:#000000\">greeting: <\/span><span style=\"background:#ffffff;color:#0000ff\">function<\/span><span style=\"background:#ffffff;color:#000000\"> (message) { <\/span><span style=\"background:#ffffff;color:#008000\">\/\/ Can be called from the server-side extension<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">output(message);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">var<\/span><span style=\"background:#ffffff;color:#000000\"> testElement = document.getElementById(<\/span><span style=\"background:#ffffff;color:#a31515\">&#8220;div1&#8221;<\/span><span style=\"background:#ffffff;color:#000000\">);<\/span>\n        <\/li>\n<li style=\"background: #f3f3f3\">\n          <span style=\"background:#ffffff;color:#000000\">browserLink.sourceMapping.selectCompleteRange(testElement);<\/span>\n        <\/li>\n<li>\n          <span style=\"background:#ffffff;color:#000000\">}<\/span>\n        <\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/div>\n<p>Now, do an F5 again. This will bring up experimental instance. Create a Razor website. In About.cshtml file add the following<\/p>\n<p>div tag:\u00a0<\/p>\n<div id=\"div1\">\n  Select Me\n<\/div>\n<p>.<\/p>\n<p>View this page in IE Browser. Now from the Dashboard, invoke the \u201cAction for SendGreetings\u201d. This will select the text in source view as shown in the image below.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/1768.image_4AFEBC2B.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/8625.image_thumb_3FD531E1.png\" width=\"644\" height=\"372\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n<p>You can download WebEssentails2013 and look at\u00a0 <a href=\"https:\/\/github.com\/madskristensen\/WebEssentials2013\/blob\/master\/EditorExtensions\/BrowserLink\/InspectMode\/InspectModeBrowserLink.js\">InspectModeBrowserLink.js<\/a> to see how to make use of the Mapping Data API.<\/p>\n<p>Hope this was useful.<\/p>\n<p>Thanks,<\/p>\n<p>Reshmi<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Browser Link feature in Visual Studio 2013 provides APIs for you to write your own Browser Link Extension. To take a look at some of the extensions, you can download Web Essentials for Visual Studio 2013 RTM. In this blog, I will give you a step by step overview of the various APIs that we [&hellip;]<\/p>\n","protected":false},"author":432,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[7434,7437,7350,7436],"class_list":["post-21981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-asp-net-4-5","tag-browser-link","tag-reshmi-mangalore","tag-visual-studio-2013"],"acf":[],"blog_post_summary":"<p>Browser Link feature in Visual Studio 2013 provides APIs for you to write your own Browser Link Extension. To take a look at some of the extensions, you can download Web Essentials for Visual Studio 2013 RTM. In this blog, I will give you a step by step overview of the various APIs that we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/21981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/432"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=21981"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/21981\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=21981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=21981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=21981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}