PageRenderTime 62ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/.config/google-chrome/Default/Extensions/jikbjpjgjmmdhcmlagappehlpiljoaop/0.5_0/amazon.js

https://bitbucket.org/kreativedev/kreativetoe
JavaScript | 128 lines | 90 code | 23 blank | 15 comment | 7 complexity | 16586b604a73cc9c89d36722213de4cf MD5 | raw file
Possible License(s): Unlicense, GPL-2.0
  1. /*
  2. * HTML5ify for Amazon Music
  3. * Makes Amazon Music preview play in ab <audio> tag instead of Flash
  4. * Author: Leonard Ehrenfried <leonard.ehrenfried@web.de>
  5. */
  6. sites.push({
  7. /*
  8. * Fetches all links that are play buttons
  9. */
  10. getAnchors : function(){
  11. var anchors = document.querySelectorAll(".trackList td.playcol>a");
  12. if(anchors.length === 0){
  13. anchors = document.querySelectorAll(".mp3Tracks td.playCol>a");
  14. }
  15. return anchors;
  16. },
  17. /**
  18. * Attach events to the anchors so that the mu3 file is downloaded
  19. */
  20. attachEvents : function(anchors){
  21. var that = this;
  22. for (var i = 0; i < anchors.length; ++i) {
  23. var anchor = anchors[i];
  24. anchor.addEventListener('click', function(e){
  25. e.stopPropagation();
  26. e.preventDefault();
  27. that.handleClick(e.target);
  28. }, true);
  29. }
  30. },
  31. handleClick : function(anchor){
  32. var that = this;
  33. //could be the image that the user clicked on
  34. if(anchor.tagName.toLowerCase() !== 'a'){
  35. anchor = anchor.parentNode;
  36. }
  37. var xhr = new XMLHttpRequest();
  38. xhr.open("GET", anchor.href, true);
  39. xhr.onreadystatechange = function() {
  40. if (xhr.readyState == 4) {
  41. that.playPreview(anchor, xhr.responseText);
  42. }
  43. };
  44. xhr.send();
  45. },
  46. /*
  47. * Creates a fully configured <audio> tag
  48. */
  49. createAudio : function(url){
  50. var audio = document.createElement("audio");
  51. audio.controls = true;
  52. audio.src = url;
  53. return audio;
  54. },
  55. createPauseButton : function(){
  56. var button = document.createElement("img");
  57. button.src = chrome.extension.getURL('/pause.png');
  58. button.style.cursor = 'pointer';
  59. return button;
  60. },
  61. playPreview : function(anchor, url){
  62. var that = this;
  63. var audio = this.createAudio(url);
  64. var pause = this.createPauseButton();
  65. pause.addEventListener("click", function(){
  66. audio.pause();
  67. that.revertPlayButton(anchor, pause);
  68. }, true);
  69. audio.addEventListener("stop", function(){
  70. that.revertPlayButton(anchor, pause);
  71. });
  72. audio.play();
  73. anchor.parentNode.appendChild(pause);
  74. anchor.style.display = 'none';
  75. },
  76. revertPlayButton : function(play, pause){
  77. play.style.display = 'inline';
  78. pause.parentNode.removeChild(pause);
  79. },
  80. insertAffiliateCode : function(){
  81. var affiliateSuffix = "tag=shizzle0a-20&linkCode=ur2&camp=1789&creative=9325";
  82. if(!window.sessionStorage.getItem("h5-amazon")){
  83. window.sessionStorage.setItem("h5-amazon", true);
  84. var url = window.location.href;
  85. if(url.indexOf("&") > -1){
  86. url += "&";
  87. }
  88. else{
  89. url += "?";
  90. }
  91. url += affiliateSuffix;
  92. var xhr = new XMLHttpRequest();
  93. xhr.open("GET", url, true);
  94. xhr.send();
  95. };
  96. },
  97. run : function(){
  98. var anchors = this.getAnchors();
  99. this.attachEvents(anchors);
  100. this.insertAffiliateCode();
  101. },
  102. shouldRun : function(){
  103. return (window.location.host.indexOf("amazon.") > -1);
  104. }
  105. });