OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 /** |
| 8 * Creates a new Navigator for navigating to links inside or outside the PDF. |
| 9 * @param {string} originalUrl The original page URL. |
| 10 * @param {Object} viewport The viewport info of the page. |
| 11 * @param {Object} paramsParser The object for URL parsing. |
| 12 */ |
| 13 function Navigator(originalUrl, viewport, paramsParser) { |
| 14 this.originalUrl_ = originalUrl; |
| 15 this.viewport_ = viewport; |
| 16 this.paramsParser_ = paramsParser; |
| 17 } |
| 18 |
| 19 Navigator.prototype = { |
| 20 /** |
| 21 * @private |
| 22 * Function to navigate to the given URL. This might involve navigating |
| 23 * within the PDF page or opening a new url (in the same tab or a new tab). |
| 24 * @param {string} url The URL to navigate to. |
| 25 * @param {boolean} newTab Whether to perform the navigation in a new tab or |
| 26 * in the current tab. |
| 27 */ |
| 28 navigate: function(url, newTab) { |
| 29 if (url.length == 0) |
| 30 return; |
| 31 // If |urlFragment| starts with '#', then it's for the same URL with a |
| 32 // different URL fragment. |
| 33 if (url.charAt(0) == '#') { |
| 34 // if '#' is already present in |originalUrl| then remove old fragment |
| 35 // and add new url fragment. |
| 36 var hashIndex = this.originalUrl_.search('#'); |
| 37 if (hashIndex != -1) |
| 38 url = this.originalUrl_.substring(0, hashIndex) + url; |
| 39 else |
| 40 url = this.originalUrl_ + url; |
| 41 } |
| 42 |
| 43 // If there's no scheme, add http. |
| 44 if (url.indexOf('://') == -1 && url.indexOf('mailto:') == -1) |
| 45 url = 'http://' + url; |
| 46 |
| 47 // Make sure inputURL starts with a valid scheme. |
| 48 if (url.indexOf('http://') != 0 && |
| 49 url.indexOf('https://') != 0 && |
| 50 url.indexOf('ftp://') != 0 && |
| 51 url.indexOf('file://') != 0 && |
| 52 url.indexOf('mailto:') != 0) { |
| 53 return; |
| 54 } |
| 55 // Make sure inputURL is not only a scheme. |
| 56 if (url == 'http://' || |
| 57 url == 'https://' || |
| 58 url == 'ftp://' || |
| 59 url == 'file://' || |
| 60 url == 'mailto:') { |
| 61 return; |
| 62 } |
| 63 |
| 64 if (newTab) { |
| 65 // Prefer the tabs API because it guarantees we can just open a new tab. |
| 66 // window.open doesn't have this guarantee. |
| 67 if (chrome.tabs) |
| 68 chrome.tabs.create({ url: url }); |
| 69 else |
| 70 window.open(url); |
| 71 } else { |
| 72 var pageNumber = |
| 73 this.paramsParser_.getViewportFromUrlParams(url).page; |
| 74 if (pageNumber != undefined) |
| 75 this.viewport_.goToPage(pageNumber); |
| 76 else |
| 77 window.location.href = url; |
| 78 } |
| 79 } |
| 80 }; |
OLD | NEW |