MediaWiki-diskusjon:Gadget-bokhylla-search.js

Sideinnholdet støttes ikke på andre språk.
Fra Wikipedia, den frie encyklopedi

Oppdatert kode[rediger kilde]

Endret litt på hvordan teksten blir seende ut, fjerner blant annet noen stoppord. Det ser ut som om søket fungerer litt bedre, det gir blant annet ikke så mye sprø treff hvis stoppord er inkludert. Muligens burde denne lista utvides, eller flyttes ut til en separat side.

Det finnes litt info om metoden Array.prototype.filter(), den lokalt implementerte metoden kan fjernes når denne blir tilgjengelig.

Her ligger det an til litt fremtidig vedlikehold, vi burde muligens ha en side hvor slikt listes. — Jeblad 5. sep. 2015 kl. 13:37 (CEST)[svar]

Oppdatert script for Bokhylla
// Manipulating the Array.prototype can lead to problems
// as we are screwing with the global prototype.
// The filter should be available by now, not sure why this
// is blocked.
if (!Array.prototype.filter2screwaround) {
  Array.prototype.filter2screwaround = function(fun/*, thisArg*/) {
    'use strict';

    if (this === void 0 || this === null) {
      throw new TypeError();
    }

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== 'function') {
      throw new TypeError();
    }

    var res = [];
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++) {
      if (i in t) {
        var val = t[i];

        // NOTE: Technically this should Object.defineProperty at
        //       the next index, as push can be affected by
        //       properties on Object.prototype and Array.prototype.
        //       But that method's new, and collisions should be
        //       rare, so use the more-compatible alternative.
        if (fun.call(thisArg, val, i, t)) {
          res.push(val);
        }
      }
    }

    return res;
  };
}

// Code for gadget to build a search for «Bokhylla».
// © John Erling Blad, Creative Commons by Attribution 3.0
$( function (){
	if (mw.config.get('wgNamespaceNumber') !== 0)
		return;
	// a very short stopwords list to avoid most failed searches
	var stopwords = {
		'ikke':true,
		'og':true,
		'i':true,
		'å':true
	};
	var portletLink = mw.util.addPortletLink(
		"p-query",
		"#",
		"Bokhylla",
		"t-bokhylla-search",
		"Litteratursøk i Bokhylla, en tjeneste fra Nasjonalbiblioteket",
		null,
		null
	);
	$('#p-query').css({display:'block'});
	$( portletLink ).click( function ( e ) {
		e.preventDefault();
		var selection = window.getSelection().toString();
		selection = selection ? selection : '';
		selection = selection
			.replace(/[^-\wæøåáčđŋšŧž]/gi,' ')
			.replace(/^\s+|\s+$/g,'')
			.split(/\s+/);
		selection = selection.filter2screwaround(function(elem){
			return !(elem in stopwords && stopwords[elem]);
		});
		selection = selection.map(encodeURI).join('+');
		var title = mw.config.get( 'wgTitle' )
			.replace(/[^-\wæøåáčđŋšŧž]/gi,' ')
			.replace(/^\s+|\s+$/g,'')
			.split(/\s+/);
		title = title.filter2screwaround(function(elem){
			return !(elem in stopwords && stopwords[elem]);
		});
		title = title.map(encodeURI).join('+');
		window.location.href = "http://www.nb.no/nbsok/search"
			+ '?page=0&menuOpen=false&instant=true&action=search&currentHit=-1'
			+ '&searchString=' + title + ( selection ? '+' + selection : '' );
	});
});
Lagt inn. Cocu (d) 5. sep. 2015 kl. 13:41 (CEST)[svar]

Rydd opp i koden[rediger kilde]

Metoden som er lagt til i prototypen skaper problemer, og er ikke lengre nødvendig. — Jeblad 28. apr. 2016 kl. 00:14 (CEST)[svar]

Oppdatert script for Bokhylla
// Code for gadget to build a search for «Bokhylla».
// © John Erling Blad, Creative Commons by Attribution 3.0
$( function (){
	if (mw.config.get('wgNamespaceNumber') !== 0)
		return;
	// a very short stopwords list to avoid most failed searches
	var stopwords = {
		'ikke':true,
		'og':true,
		'i':true,
		'å':true
	};
	var portletLink = mw.util.addPortletLink(
		"p-query",
		"#",
		"Bokhylla",
		"t-bokhylla-search",
		"Litteratursøk i Bokhylla, en tjeneste fra Nasjonalbiblioteket",
		null,
		null
	);
	$('#p-query').css({display:'block'});
	$( portletLink ).click( function ( e ) {
		e.preventDefault();
		var selection = window.getSelection().toString();
		selection = selection ? selection : '';
		selection = selection
			.replace(/[^-\wæøåáčđŋšŧž]/gi,' ')
			.replace(/^\s+|\s+$/g,'')
			.split(/\s+/);
		selection = selection.filter(function(elem){
			return !(elem in stopwords && stopwords[elem]);
		});
		selection = selection.map(encodeURI).join('+');
		var title = mw.config.get( 'wgTitle' )
			.replace(/[^-\wæøåáčđŋšŧž]/gi,' ')
			.replace(/^\s+|\s+$/g,'')
			.split(/\s+/);
		title = title.filter(function(elem){
			return !(elem in stopwords && stopwords[elem]);
		});
		title = title.map(encodeURI).join('+');
		window.location.href = "http://www.nb.no/nbsok/search"
			+ '?page=0&menuOpen=false&instant=true&action=search&currentHit=-1'
			+ '&searchString=' + title + ( selection ? '+' + selection : '' );
	});
});
Utført Cocu (d) 28. apr. 2016 kl. 09:37 (CEST)[svar]

Bokhylla er i endring, oppdatering må legges inn[rediger kilde]

Bytt ut kodeblokken

		window.location.href = "http://www.nb.no/nbsok/search"
			+ '?page=0&menuOpen=false&instant=true&action=search&currentHit=-1'
			+ '&searchString=' + title + ( selection ? '+' + selection : '' );

med den nye koden

		window.location.href = "https://www.nb.no/search"
			+ '?q=' + title + ( selection ? '+' + selection : '' );

Skal gjøre noen kontrollsøk når endringen er lagt inn. — Jeblad 7. apr. 2018 kl. 19:43 (CEST)[svar]

Gjort. --Apple farmer (diskusjon) 7. apr. 2018 kl. 20:02 (CEST)[svar]

Her er det nok mere småpirk. Legg inn hele koden, det er enklere enn at jeg skriver hva som må endres. (Det er bare tre '+' som skal være '%20') — Jeblad 7. apr. 2018 kl. 20:52 (CEST)[svar]

// Code for gadget to build a search for «Bokhylla».
// © John Erling Blad, Creative Commons by Attribution 3.0
$( function (){
	if (mw.config.get('wgNamespaceNumber') !== 0)
		return;
	// a very short stopwords list to avoid most failed searches
	var stopwords = {
		'ikke':true,
		'og':true,
		'i':true,
		'å':true
	};
	var portletLink = mw.util.addPortletLink(
		"p-query",
		"#",
		"Bokhylla",
		"t-bokhylla-search",
		"Litteratursøk i Bokhylla, en tjeneste fra Nasjonalbiblioteket",
		null,
		null
	);
	$('#p-query').css({display:'block'});
	$( portletLink ).click( function ( e ) {
		e.preventDefault();
		var selection = window.getSelection().toString();
		selection = selection ? selection : '';
		selection = selection
			.replace(/[^-\wæøåáčđŋšŧž]/gi,' ')
			.replace(/^\s+|\s+$/g,'')
			.split(/\s+/);
		selection = selection.filter(function(elem){
			return !(elem in stopwords && stopwords[elem]);
		});
		selection = selection.map(encodeURI).join('%20');
		var title = mw.config.get( 'wgTitle' )
			.replace(/[^-\wæøåáčđŋšŧž]/gi,' ')
			.replace(/^\s+|\s+$/g,'')
			.split(/\s+/);
		title = title.filter(function(elem){
			return !(elem in stopwords && stopwords[elem]);
		});
		title = title.map(encodeURI).join('%20');
		window.location.href = "https://www.nb.no/search"
			+ '?q=' + title + ( selection ? '%20' + selection : '' );
	});
});
Gjort. mvh, --Apple farmer (diskusjon) 7. apr. 2018 kl. 21:08 (CEST)[svar]