Benutzer:Lustiger seth/check geoblocked.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
// ==UserScript==
// @name        wikipedia-kram
// @namespace   wikipedia
// @description checks whether srf.ch urls are geoblocked
// @include     https://de.wikipedia.org/*
// @include     https://en.wikipedia.org/*
// @version     1.0.0
// @grant       none
// ==/UserScript==

// usage:
// this script won't work as script as a user script inside of mediawiki, because of CORS.
// so you have to use it via greasemonkey.
//
// details:
// * this snippet adds a button "check geoblocked" to the general toolbar menu 
//   (right hand side at vector-2022 skin).
// * if the button "check geoblocked" is clicked, then a prompt appears asking the 
//   user for an url
// * after the user inserts an url (such as 
//   "https://www.srf.ch/play/tv/redirect/detail/bbfec159-e7a3-45b6-a107-da95bf45e20c")
//   and presses "ok", the script will fetch information on whether the target media
//   is geoblocked.
// * finally the use will get an alert with the result ("true" or "false").

function http_request(url, callback){
	const xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState === 4 && xmlhttp.status == "200"){
			return callback(xmlhttp.responseText);
		}
	}
	xmlhttp.send(null);
}

function check_geoblocked(){
	let url = window.prompt("url?");
	let urn = "";
	let m = url.match(/[?&]urn=([^?&]+)/);
	// https://www.srf.ch/...1959?urn=urn:srf:video:bbfec159-e7a3-45b6-a107-da95bf45e20c
	if(m){
		urn = m[1];
	}
	// https://www.srf.ch/play/tv/redirect/detail/bbfec159-e7a3-45b6-a107-da95bf45e20c
	m = url.match(/\/detail\/([a-f0-9-]{32,})(?:[?&]|$)/);
	if(m){
		urn = "urn:srf:video:" +  m[1];
	}
	if(urn !== ""){
		url = "https://il.srgssr.ch/integrationlayer/2.0/mediaComposition/byUrn/" 
			+ urn +".json";
	  const callback = function(http_content){
			const data = JSON.parse(http_content);
      const is_geoblocked = data.chapterList[0].analyticsMetadata.media_is_geoblocked;
			alert("media_is_geoblocked: " + is_geoblocked);
			return is_geoblocked;
		}
    http_request(url, callback);
	}
}

function install_check_geoblocked_button(){
	const button = document.createElement("li");
	const vector_tb = document.getElementById("p-tb").getElementsByClassName("vector-menu-content-list")[0]; 
	button.className = Array.from(vector_tb.childNodes).at(-1).className; // "mw-list-item mw-list-item-js";
	button.innerHTML = '<a>check geoblocked</a>';
	button.addEventListener("click", check_geoblocked);
	vector_tb.appendChild(button);
}

install_check_geoblocked_button();

// does not work, because of CORS:
//$.when(mw.loader.using('mediawiki.util'), $.ready).then(function (){
//	mw.util.addPortletLink(
//		'p-tb', "javascript:check_geoblocked();", "check geoblocked");
//} );