Share Bandcamp.com lyrics and songtexts on Genius.com

Adds a link above the lyrics on bandcamp to share lyrics to genius.com. It then automatically copies all the available information (title, artist, release date, ...) to genius.com

目前为 2017-11-17 提交的版本。查看 最新版本

// ==UserScript==
// @name        Share Bandcamp.com lyrics and songtexts on Genius.com
// @description Adds a link above the lyrics on bandcamp to share lyrics to genius.com. It then automatically copies all the available information (title, artist, release date, ...) to genius.com
// @homepageURL https://openuserjs.org/scripts/cuzi/Share_Bandcamp.com_lyrics_and_songtexts_on_Genius.com
// @namespace   cuzi
// @version     4
// @license     GPL-3.0
// @include     https://*.bandcamp.com/*
// @include     https://bandcamp.com/private/*
// @include     http://genius.com/new
// @include     https://genius.com/new
// @require     https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @require     https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @grant       GM_openInTab
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       unsafeWindow
// @grant       GM.openInTab
// @grant       GM.setValue
// @grant       GM.getValue
// ==/UserScript==

(async function() {
"use strict";

var sid = 0;

function bc_start() {
  // Add links to lyrics info
  var lyricsdiv = document.querySelectorAll(".collapsibleLyrics.lyricsText");
  for(var i = 0; i < lyricsdiv.length; i++) {
      var a = $('<div><a href="#genius">Share on genius.com</a></div>').click(bc_openGenius).prependTo(lyricsdiv[i].parentNode);
  }
}

async function bc_openGenius(ev) {
  // License
  if($("#license")) {
    if(!confirm("You need to respect the license of this work.\nIf in doubt, ask the copyright proprietor.\nShort version of the license:\n\n"+$.trim($("#license").text())+"\n\nMore info here:\n"+$("#license a").attr("href")+"\n\nOk?")) {
      return;
    }
  }
  
  // Identify song
  var tr_lyrics = $(this.parentNode.parentNode);
  var tr_song = tr_lyrics.prev("tr");
  
  // Initiate handshake
  await GM.setValue("g_acknowledgement", 0); // Receive acknowledgement here
  await GM.setValue("bc_waiting",true); // Request acknowledgement
  
  // Open tab 
  GM.openInTab("http://genius.com/new", false);
    
  // Wait for acknowledgement of handshake:
  var iv = window.setInterval(async function() {
    sid = await GM.getValue("g_acknowledgement", 0);
    if(sid) {
      clearInterval(iv);
      bc_sendData(tr_lyrics, tr_song);
      // Clean up:
      GM.setValue("g_acknowledgement", 0);
    }
  },100);
}

function bc_sendData(tr_lyrics, tr_song) {
  // Collect data and send to genius window

  var releaseDate = new Date(unsafeWindow.TralbumData.album_release_date);
  
  var direct = {
    "song_primary_artist" : unsafeWindow.TralbumData.artist,
    "song_title" : $.trim(tr_song.find('*[itemprop="name"]').text()),
    "song_lyrics" : $.trim(tr_lyrics.find(".lyricsText").text().replace(/\n\n/g, "\n")),
    "song_featured_artists" : "",
    "song_producer_artists" : "",
    "song_writer_artists" : "",
    "song_release_date_1i" : releaseDate.getFullYear(),
    "song_release_date_2i" : releaseDate.getMonth()+1,
    "song_release_date_3i" : releaseDate.getDate(),
  };
  var other = {
    "album_name" : unsafeWindow.TralbumData.current.title,
    "about" : $(".tralbumData.tralbum-about").html() || "",
    "credits" : $(".tralbumData.tralbum-credits").html() || "",
    "tags" : Array.map($(".tralbumData.tralbum-tags a"),e => e.text).join(", ") || "",
    "albumart" : $(".popupImage").get(0).href
  };
  
  GM.setValue("bc_data",JSON.stringify({
    "sid" : sid,
    "direct" : direct,
    "other" : other
  }));
  
}



async function g_start() {
  // Wait for a first message/handshake from bandcamp
  if(await GM.getValue("bc_waiting", false)) {
    sid = 1+Math.random();
    await GM.setValue("bc_waiting", false); // Clean up
    await GM.setValue("g_acknowledgement", sid); // Send acknowledgement
    // Start receiving data
    g_receiveData();
  }
}

function g_receiveData() {
  // Wait for the data from bandcamp
  var iv = window.setInterval(async function() {
    var response = JSON.parse(await GM.getValue("bc_data", "{}"));
    if("sid" in response && response.sid == sid) {
      clearInterval(iv);
      
      g_fillForm(response);
      
      // Clean up
      GM.setValue("bc_data", "{}");
    }
  },100);
  
  // Click on "Add album" to generate a new album input field
  var evt = document.createEvent("MouseEvents");
  evt.initEvent("click", true, true);
  document.getElementById("add_album_name").dispatchEvent(evt);
}

function g_fillForm(rsp) {
  // Directly enter data by id
  for(var id in rsp.direct) {
    $(document.getElementById(id)).val(rsp.direct[id]);
  }
  
  // Create keyup event on song name, to generate the warning about duplicates

    var evt = document.createEvent("KeyboardEvent");
    evt.initKeyEvent ("keyup", true, true, window, 0, 0, 0, 0, 0, "e".charCodeAt(0)) 
    document.getElementById("song_primary_artist").dispatchEvent(evt);

  
  // Album name
  $(".album_name.add_album input").first().val(rsp.other.album_name);
  
  // Tags
  $("<div><b>Tags:</b><br>" +  rsp.other.tags + '<div style="position:absolute;font-size:xx-small;color:green;top:-5px; right:0px;">bandcamp info</div></div>').css({
    position:"absolute",
    left : 0,
    top : $(".primary_tag_chooser").offset().top,
    maxWidth: (($(document.body).width() - $("#container").width() - 15) / 2),
    background: "#DDB",
    color: "black",
    padding: "5px"
  }).appendTo(document.body);
  
  // Credits & About & Song title
  var credits = $("<div><b>Title:</b><br>" + rsp.direct.song_title + "<br><b>Credits:</b><div>" +  rsp.other.credits + "</div><b>About:</b><div>" +  rsp.other.about + '</div><div style="position:absolute;font-size:xx-small;color:green;top:-5px; right:0px;">bandcamp info</div></div>').css({
    position:"absolute",
    left : 0,
    top : $(".add_song_page-header:contains('Meta')").offset().top,
    maxWidth: (($(document.body).width() - $("#container").width() - 30) / 2),
    maxHeight: 500,
    overflow:"auto",
    background: "#DDB",
    color: "black",
    padding: "5px"
  }).appendTo(document.body);
  
  // Reposition credits on lyrics change
  var $song_lyrics = $("#song_lyrics");
  var oldheight = $song_lyrics.height();
  $song_lyrics.change(function() {
    if($song_lyrics.height() != oldheight) {
      oldheight = $song_lyrics.height();
      credits.css("top",$(".add_song_page-header:contains('Meta')").offset().top);
    }
  });
  
}


  if(document.location.href.endsWith("genius.com/new")) {
    window.setTimeout(g_start,1000);
  } else if(unsafeWindow.TralbumData) {
    window.setTimeout(bc_start,1000);
  }
})();