2017-01-17 00:15:03 +01:00
debugmsg = false ;
debugmsg _imdb = true ;
2016-12-29 19:41:54 +01:00
url _changed = false ;
2017-01-17 00:15:03 +01:00
if ( debugmsg ) {
console . log ( ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " ) ;
console . log ( "\nLoading ultrawidify background script (uw-bg)\nIf you can see this, extension at least tried to load\n\nRandom number: " , Math . floor ( Math . random ( ) * 20 ) + 1 , "\n" ) ;
console . log ( ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " ) ;
}
2016-12-29 19:41:54 +01:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * script - related stuff starts here * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
2016-12-21 23:25:05 +01:00
function gibActiveTab ( ) {
return browser . tabs . query ( { active : true , currentWindow : true } ) ;
}
2017-01-04 23:23:41 +01:00
var page _change _msg _count = 0 ;
2016-12-21 23:25:05 +01:00
function notifyChange ( ) {
2016-12-29 19:41:54 +01:00
if ( debugmsg )
2017-01-04 23:23:41 +01:00
console . log ( "uw-bg::tab updated. seq:" , page _change _msg _count ++ ) ;
2016-12-29 19:41:54 +01:00
2017-01-02 17:45:51 +01:00
browser . tabs . query ( { active : true , currentWindow : true } , function ( tabs ) {
browser . tabs . sendMessage ( tabs [ 0 ] . id , { message : "page-change" } ) ;
} ) ;
2016-12-21 23:25:05 +01:00
}
2017-01-17 00:15:03 +01:00
browser . tabs . onUpdated . addListener ( notifyChange ) ;
2016-12-21 23:25:05 +01:00
2017-01-17 00:15:03 +01:00
//BEGIN Goldberg machine that gets aspect ratio data off imdb
2016-12-21 23:25:05 +01:00
2017-01-17 00:15:03 +01:00
function getAspectRatio ( title , sender _tab ) {
// presledki morajo biti zamenjani s +
// spaces need to be replaced with +
var rektitle = title . replace ( / /g , '+' ) ;
// Zdaj lahko pošljemo zahtevek na omdbapi
// now we can send a request to omdbapi
httpGET ( "http://www.omdbapi.com/?t=" + rektitle ,
function ( response , sender _tab ) {
if ( debugmsg || debugmsg _imdb )
console . log ( "uw-bg::getAspectRatio | omdbapi gave us this: " , response ) ;
var info = JSON . parse ( response ) ;
if ( debugmsg || debugmsg _imdb ) {
console . log ( "uw-bg::getAspectRatio | movie title: »»" , info . Title , "«« | imdb ID:" , info . imdbID , "\nTrying to get technical specs off IMDB" ) ;
}
httpGET ( "https://www.imdb.com/title/" + info . imdbID + "/technical" ,
function ( response , sender _tab ) {
var lines = response . split ( '\n' ) ;
if ( debugmsg || debugmsg _imdb ) {
console . log ( "uw-bg::getAspectRatio | we just got something off IMDB, it's" , lines . length , "long. Here's what we got:\n" , response ) ;
}
// IMDB nam zraven da veliko nepotrebnega sranja. Na testni strani je bil relevanten podatek
// 700+ (!) vrstic globoko. Stvar, ki nam jo da IMDB ima 1500+ vrstic. Iskanje bomo zato začeli
// od sredine
//
// IMDB gives us a lot of unnecessary shit along with the data we want. On our test page the
// relevant data was buried 700+ lines deep (1500+ lines total). Because we don't want to
// pointlessly search half the page, the best place to start seems to be the middle.
var lines _nr = lines . length ;
if ( lines _nr % 2 == 1 )
++ lines _nr ;
var i = lines _nr / 2 ;
var j = i ;
var ar _found = 0 ;
while ( i > 400 && j < lines _nr ) {
if ( lines [ i ] . indexOf ( "Aspect Ratio" ) != - 1 ) {
if ( debugmsg || debugmsg _imdb )
console . log ( "uw-bg::getAspectRatio | »Aspect Ratio« has been found on line" , i , " — searching for aspect ratio ..." ) ;
ar _found = i ;
break ;
}
if ( lines [ j ] . indexOf ( "Aspect Ratio" ) != - 1 ) {
if ( debugmsg || debugmsg _imdb )
console . log ( "uw-bg::getAspectRatio | »Aspect Ratio« has been found on line" , j , " — searching for aspect ratio ..." ) ;
ar _found = j ;
break ;
}
-- i ;
++ j ;
}
if ( ar _found ) {
var ar _limit = ar _found + 5 ;
for ( var i = ar _found ; i < ar _limit ; ++ i ) {
if ( debugmsg || debugmsg _imdb )
console . log ( "uw-bg::getAspectRatio | scanning lines for aspect ratio number. Line:" , lines [ i ] , "has ar:" , lines [ i ] . indexOf ( ":" ) ) ;
if ( lines [ i ] . indexOf ( ":" ) != - 1 ) {
// To pomeni, da smo našli razmerje stranic. gg ez
// This means we found our aspect ratio, gg ez
var ar = lines [ i ] . trim ( ) . split ( ":" ) ;
ar [ 0 ] = ar [ 0 ] . trim ( ) ;
ar [ 1 ] = ar [ 1 ] . trim ( ) ;
if ( debugmsg || debugmsg _imdb )
console . log ( "uw-bg::getAspectRatio | Aspect ratio found:" , ar [ 0 ] , ":" , ar [ 1 ] ) ;
// Pa povejmo to naši strani:
// Let's break the news:
browser . tabs . sendMessage ( sender _tab . id , { type : "arInfo" , arx : ar [ 0 ] , ary : ar [ 1 ] } ) ;
}
}
}
else
if ( debugmsg || debugmsg _imdb )
console . log ( "uw-bg::getAspectRatio | Aspect ratio hasn't been found" ) ;
} ,
sender _tab
) ; //httpGET end
} ,
sender _tab
) ; //httpGET end
}
2017-01-04 23:23:41 +01:00
2017-01-17 00:15:03 +01:00
function httpGET ( url , callback , callback _args ) {
var rek = new XMLHttpRequest ( ) ;
rek . onreadystatechange = function ( ) {
if ( rek . readyState == 4 && rek . status == 200 ) {
callback ( rek . responseText , callback _args ) ;
}
}
rek . open ( "GET" , url , true ) ;
rek . send ( null ) ;
}
//END 3rd party snooping for aspect ratios
browser . runtime . onMessage . addListener ( function ( request , sender , sendResponse ) {
//Stvari delamo samo, če ima naše sporočilce tip
//We only do stuff if our message has a type
if ( request . type ) {
if ( request . type == "debug" ) {
console . log ( "uw-bg::onMessage | got a message. It was a debugging message. Here's the full message:" , request ) ;
}
if ( request . type == "gibAspectRatio" ) {
if ( debugmsg || debugmsg _imdb )
console . log ( "uw-bg::onMessage | got a message, we want to set aspect ratio. message:" , request , "sender:" , sender ) ;
var result = getAspectRatio ( request . title , sender . tab ) ;
}
}
2016-12-21 23:25:05 +01:00
} ) ;