background.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. Copyright 2014 Mozilla Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. "use strict";
  14. (function PageActionClosure() {
  15. /**
  16. * @param {number} tabId - ID of tab where the page action will be shown.
  17. * @param {string} url - URL to be displayed in page action.
  18. */
  19. function showPageAction(tabId, displayUrl) {
  20. // rewriteUrlClosure in viewer.js ensures that the URL looks like
  21. // chrome-extension://[extensionid]/http://example.com/file.pdf
  22. var url = /^chrome-extension:\/\/[a-p]{32}\/([^#]+)/.exec(displayUrl);
  23. if (url) {
  24. url = url[1];
  25. chrome.pageAction.setPopup({
  26. tabId,
  27. popup: "/pageAction/popup.html?file=" + encodeURIComponent(url),
  28. });
  29. chrome.pageAction.show(tabId);
  30. } else {
  31. console.log("Unable to get PDF url from " + displayUrl);
  32. }
  33. }
  34. chrome.runtime.onMessage.addListener(function (message, sender) {
  35. if (message === "showPageAction" && sender.tab) {
  36. showPageAction(sender.tab.id, sender.tab.url);
  37. }
  38. });
  39. })();