PageSourceSearch

https://topplista.no/js/search.js

js topplista.no collected 2026-10-02 12:59:10 UTC 3,624 bytes, 131 lines download raw bytes

1summaryInclude=60;
2
3var pages = false;
4var fuse = false;
5var result = false;
6var fuseOptions = {
7  shouldSort: true,
8  includeMatches: true,
9  threshold: 0.0,
10  tokenize: true,
11  ignorelocation: true,
12  maxPatternLength: 32,
13  minMatchCharLength: 2,
14  keys: [
15    {name:"title",weight:1},
16    {name:"tracks",weight:1},
17  ]
18};
19
20
21function doSearch(x,file){
22	searchQuery = $("#search-query").val()
23	if (searchQuery != ''){
24		document.body.scrollTop = 0; // For Safari
25		document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
26		$('#search-results').html("søker...");
27		executeSearch(searchQuery, file);
28	}
29}
30
31function executeSearch(searchQuery,file){
32
33	if (pages == false){
34		$.getJSON( file, function( data ) {
35			pages = data;
36			fuse = new Fuse(pages, fuseOptions);
37			result = fuse.search(searchQuery);
38			console.log({"matches":result});
39			if(result.length > 0){
40				populateResults(result);
41			}else{
42				$('#search-results').html("<p>No matches found</p>");
43			}
44		});
45	 }else{
46		result = fuse.search(searchQuery);
47		console.log({"matches":result});
48		if(result.length > 0){
49			populateResults(result);
50		}else{
51			$('#search-results').html("<p>No matches found</p>");
52		}
53	 }
54}
55
56function normalize(text) {
57    return text.trim().toLowerCase().replace(/[^\w\s]/g, '');
58}
59
60function populateResults(result) {
61    $('#search-results').html('');
62    var query = $("#search-query").val().toLowerCase();
63    $("#search-query").val('');
64
65    $.each(result, function (key, value) {
66        let matchedContent = '';
67        const title = value.item.title;
68
69        if (value.matches && value.matches.length > 0) {
70            matchedContent = value.matches[0].value;
71        } else {
72            matchedContent = title;
73        }
74
75        // Build snippet
76        let snippet = matchedContent.substring(0, summaryInclude * 2);
77
78        // Skip snippet if it's the same as title
79        if (normalize(snippet) === normalize(title)) {
80            snippet = '';
81        } else {
82            snippet = `<p>${snippet}</p>`;
83        }
84
85        const templateDefinition = $('#search-result-template').html();
86        const output = render(templateDefinition, {
87            key: key,
88            title: title,
89            link: value.item.permalink,
90            snippet: snippet
91        });
92
93        $('#search-results').append($.parseHTML(output));
94    });
95}
96
97function param(name) {
98    return decodeURIComponent((location.search.split(name + '=')[1] || '').split('&')[0]).replace(/\+/g, ' ');
99}
100
101function render(templateString, data) {
102  var conditionalMatches,conditionalPattern,copy;
103  conditionalPattern = /\$\{\s*isset ([a-zA-Z]*) \s*\}(.*)\$\{\s*end\s*}/g;
104  //since loop below depends on re.lastInxdex, we use a copy to capture any manipulations whilst inside the loop
105  copy = templateString;
106  while ((conditionalMatches = conditionalPattern.exec(templateString)) !== null) {
107    if(data[conditionalMatches[1]]){
108      //valid key, remove conditionals, leave contents.
109      copy = copy.replace(conditionalMatches[0],conditionalMatches[2]);
110    }else{
111      //not valid, remove entire section
112      copy = copy.replace(conditionalMatches[0],'');
113    }
114  }
115  templateString = copy;
116  //now any conditionals removed we can do simple substitution
117  var key, find, re;
118  for (key in data) {
119    find = '\\$\\{\\s*' + key + '\\s*\\}';
120    re = new RegExp(find, 'g');
121    templateString = templateString.replace(re, data[key]);
122  }
123  return templateString;
124}
125
126
127/* function render(template, data) {
128  return template.replace(/${([\w]+)}/g, function(_, key) {
129    return key in data ? data[key] : '';
130  });
131} */

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.