@@ -501,23 +501,6 @@ function getWords(text, search) {
501
501
return matchedPercentage > 0.5 ;
502
502
}
503
503
504
- document . querySelector ( ".searchbar" ) . addEventListener ( "input" , function ( ) {
505
- if ( document . querySelector ( ".welcome" ) ) {
506
- if ( document . querySelector ( ".searchbar" ) . value ) {
507
- document . querySelector ( ".welcome" ) . style . display = "none" ;
508
- } else {
509
- document . querySelector ( ".welcome" ) . style . display = null ;
510
- }
511
- }
512
- document . querySelectorAll ( ".feature" ) . forEach ( function ( el ) {
513
- if ( getWords ( el . innerText , document . querySelector ( ".searchbar" ) . value ) ) {
514
- el . style . display = null ;
515
- } else {
516
- el . style . display = "none" ;
517
- }
518
- } ) ;
519
- } ) ;
520
-
521
504
if ( document . querySelector ( ".settingsButton" ) ) {
522
505
document
523
506
. querySelector ( ".settingsButton" )
@@ -562,6 +545,8 @@ async function returnFeatureCode() {
562
545
}
563
546
}
564
547
548
+ let FEATURES = [ ] ;
549
+
565
550
async function getFeatures ( ) {
566
551
var languageData = await getFeatureLanguageData ( ) ;
567
552
const settings = ( await chrome . storage . sync . get ( "features" ) ) . features || "" ;
@@ -587,6 +572,13 @@ async function getFeatures() {
587
572
}
588
573
div . dataset . type = feature . type . join ( "" ) ;
589
574
575
+ FEATURES . push ( {
576
+ title : feature . title ,
577
+ description : feature . description ,
578
+ id : feature . id ,
579
+ new : feature . versionAdded === "v" + chrome . runtime . getManifest ( ) . version ,
580
+ } ) ;
581
+
590
582
var h3 = document . createElement ( "h3" ) ;
591
583
h3 . textContent =
592
584
languageData [ feature . id + "/title" ] ?. message || feature . title ;
@@ -693,16 +685,16 @@ async function getFeatures() {
693
685
694
686
if ( typeof option . type === "string" ) {
695
687
let OPTION_TYPES = {
696
- " string" : 0 ,
697
- " boolean" : 1 ,
698
- " number" : 2 ,
699
- " color" : 3 ,
700
- " select" : 4 ,
701
- }
702
- option . type = OPTION_TYPES [ option . type ]
688
+ string : 0 ,
689
+ boolean : 1 ,
690
+ number : 2 ,
691
+ color : 3 ,
692
+ select : 4 ,
693
+ } ;
694
+ option . type = OPTION_TYPES [ option . type ] ;
703
695
}
704
696
705
- let type = option . type
697
+ let type = option . type ;
706
698
if ( type === 4 ) {
707
699
var optionDiv = document . createElement ( "div" ) ;
708
700
optionDiv . className = "option" ;
@@ -781,7 +773,7 @@ async function getFeatures() {
781
773
] ;
782
774
input . value = optionData || "" ;
783
775
input . placeholder = `Enter ${ input . type } ` ;
784
- input . dataset . validators = JSON . stringify ( option . validators || { } )
776
+ input . dataset . validators = JSON . stringify ( option . validators || { } ) ;
785
777
var optionDiv = document . createElement ( "div" ) ;
786
778
optionDiv . className = "option" ;
787
779
var label = document . createElement ( "label" ) ;
@@ -812,7 +804,7 @@ async function getFeatures() {
812
804
var validation = JSON . parse ( atob ( this . dataset . validation ) ) ;
813
805
var ready = true ;
814
806
var input = this ;
815
- let validators = JSON . parse ( this . dataset . validators )
807
+ let validators = JSON . parse ( this . dataset . validators ) ;
816
808
validation . forEach ( function ( validate ) {
817
809
if ( ready ) {
818
810
input . style . outline = "none" ;
@@ -838,12 +830,12 @@ async function getFeatures() {
838
830
if ( ready ) {
839
831
if ( validators . min ) {
840
832
if ( this . value < validators . min ) {
841
- this . value = validators . min
833
+ this . value = validators . min ;
842
834
}
843
835
}
844
836
if ( validators . max ) {
845
837
if ( this . value > validators . max ) {
846
- this . value = validators . max
838
+ this . value = validators . max ;
847
839
}
848
840
}
849
841
if ( this . type !== "checkbox" ) {
@@ -1502,3 +1494,103 @@ document
1502
1494
} ) ;
1503
1495
}
1504
1496
} ) ;
1497
+
1498
+ function searchAndSort ( query ) {
1499
+ const lowerCaseQuery = query . toLowerCase ( ) ;
1500
+
1501
+ const scoredData = FEATURES . map ( ( item ) => {
1502
+ const nameMatches = item . title . toLowerCase ( ) . includes ( lowerCaseQuery )
1503
+ ? 1
1504
+ : 0 ;
1505
+ const descriptionMatches = item . description
1506
+ . toLowerCase ( )
1507
+ . includes ( lowerCaseQuery )
1508
+ ? 1
1509
+ : 0 ;
1510
+ const totalMatches = nameMatches + descriptionMatches ;
1511
+
1512
+ return {
1513
+ ...item ,
1514
+ relevance : totalMatches ,
1515
+ } ;
1516
+ } ) ;
1517
+
1518
+ scoredData . sort ( ( a , b ) => b . relevance - a . relevance ) ;
1519
+
1520
+ const filteredData = scoredData . filter ( ( item ) => item . relevance > 0 ) ;
1521
+
1522
+ return filteredData ;
1523
+ }
1524
+
1525
+ let searchbar = document . querySelector ( ".searchbar" ) ;
1526
+ searchbar . addEventListener ( "input" , function ( ) {
1527
+ if ( document . querySelector ( ".welcome" ) ) {
1528
+ if ( searchbar . value ) {
1529
+ document . querySelector ( ".welcome" ) . style . display = "none" ;
1530
+ } else {
1531
+ document . querySelector ( ".welcome" ) . style . display = null ;
1532
+ }
1533
+ }
1534
+ if ( ! searchbar . value ) {
1535
+ if ( document . querySelector ( 'h1[data-id="all-features-text"]' ) ) {
1536
+ document . querySelector ( 'h1[data-id="all-features-text"]' ) . textContent =
1537
+ "All features" ;
1538
+
1539
+ document . querySelector ( ".suggested h1" ) . style . display = null ;
1540
+ document . querySelector ( ".suggested-features" ) . style . display = null ;
1541
+ }
1542
+
1543
+ document . querySelectorAll ( ".feature" ) . forEach ( function ( el ) {
1544
+ el . style . display = null ;
1545
+ } ) ;
1546
+
1547
+ for ( var i in FEATURES ) {
1548
+ document
1549
+ . querySelector ( ".settings" )
1550
+ . appendChild (
1551
+ document . querySelector (
1552
+ `div.settings .feature[data-id=${ FEATURES [ i ] . id } ]`
1553
+ )
1554
+ ) ;
1555
+ }
1556
+
1557
+ for ( var i in FEATURES . filter ( ( el ) => el . new ) ) {
1558
+ document
1559
+ . querySelector ( ".settings" )
1560
+ . prepend (
1561
+ document . querySelector (
1562
+ `div.settings .feature[data-id=${
1563
+ FEATURES . filter ( ( el ) => el . new ) [ i ] . id
1564
+ } ]`
1565
+ )
1566
+ ) ;
1567
+ }
1568
+ } else {
1569
+ if ( document . querySelector ( 'h1[data-id="all-features-text"]' ) ) {
1570
+ document . querySelector ( 'h1[data-id="all-features-text"]' ) . textContent =
1571
+ "Results" ;
1572
+
1573
+ document . querySelector ( ".suggested h1" ) . style . display = "none" ;
1574
+ document . querySelector ( ".suggested-features" ) . style . display = "none" ;
1575
+ }
1576
+
1577
+ let results = searchAndSort ( searchbar . value ) ;
1578
+ document . querySelectorAll ( ".feature" ) . forEach ( function ( el ) {
1579
+ if ( results . find ( ( result ) => result . id === el . dataset . id ) ) {
1580
+ el . style . display = null ;
1581
+ } else {
1582
+ el . style . display = "none" ;
1583
+ }
1584
+ } ) ;
1585
+
1586
+ for ( var i in results ) {
1587
+ document
1588
+ . querySelector ( ".settings" )
1589
+ . appendChild (
1590
+ document . querySelector (
1591
+ `div.settings .feature[data-id=${ results [ i ] . id } ]`
1592
+ )
1593
+ ) ;
1594
+ }
1595
+ }
1596
+ } ) ;
0 commit comments