Open External Links With Default Browser
-
<a href="https://www.google.com/" target="_blank">Google</a>
By default, this will open google in internet explorer if called from the webdialog (even if my default browser is Chrome). I would like to know how to tweak that to open links with default browser. SketchyUcation PluginStore opens links with default browser, which means it's possible to tweak it. Can anyone give an advice on how to do that? -
Pass the link back to the ruby plugin, then use
UI.openURL()
-
What Jim said. I usually put a generic hook in my webdialogs that automatically redirect all links like that: https://github.com/thomthom/SKUI/blob/master/src/SKUI/js/ui.js#L96
-
Here is the code snippets, just so if anyone is interested later
This goes to JavaScript:<span class="syntaxdefault"><br /></span><span class="syntaxcomment">// Determines whether string contains another string.<br /></span><span class="syntaxdefault">String</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">prototype</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">contains </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> function</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">it</span><span class="syntaxkeyword">){<br /></span><span class="syntaxdefault"> return this</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">indexOf</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">it</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">;<br />};<br /><br /></span><span class="syntaxcomment">// Calls a Ruby callback.<br /></span><span class="syntaxdefault">function callback</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> data</span><span class="syntaxkeyword">){<br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">data</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> data </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault"> window</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">location</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">href </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'skp;'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> name </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> </span><span class="syntaxstring">'@'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> data</span><span class="syntaxkeyword">;<br />}<br /><br /></span><span class="syntaxcomment">// Open links with default browser.<br /></span><span class="syntaxkeyword">$(</span><span class="syntaxdefault">document</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">on</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'click'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'a[href]'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> function</span><span class="syntaxkeyword">(){<br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">this</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">href</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">contains</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"http"</span><span class="syntaxkeyword">)){<br /></span><span class="syntaxdefault"> callback</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'open_link'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> this</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">href</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault"> return false</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br />});<br /> </span><span class="syntaxdefault"></span>
This goes to Ruby:
<span class="syntaxdefault"><br /></span><span class="syntaxkeyword">@</span><span class="syntaxdefault">dlg</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_action_callback</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'open_link'</span><span class="syntaxkeyword">){</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">dlg</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> params</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">openURL</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">params</span><span class="syntaxkeyword">)<br />}<br /> </span><span class="syntaxdefault"></span>
Advertisement