@@ -59,59 +59,76 @@ function displayPreloader(sectionTitle, domain) {
59
59
function handlePolicyLinks ( ) {
60
60
const keywords = [ 'privacy' , 'terms' , 'return' , 'shipping' , 'legal' , 'cookie' ] ;
61
61
const currentDomain = rootDomain ( new URL ( window . location . href ) . hostname ) ;
62
- let linksMap = { } ;
63
62
let summarizedLinks = JSON . parse ( localStorage . getItem ( 'summarizedLinks' ) || '{}' ) ;
64
63
65
64
// get chrome.storage.local of auto_summaries, and if it exists, set it to autoSummaries, else set to false
66
65
chrome . storage . local . get ( [ 'autoSummaries' ] , function ( result ) {
67
66
let autoSummaries = result . autoSummaries || false ;
68
- if ( autoSummaries ) {
69
- keywords . forEach ( keyword => {
70
- const foundLinks = Array . from ( document . querySelectorAll ( 'a' ) ) . filter ( link => {
71
- return ( link . href . toLowerCase ( ) . includes ( keyword ) || link . innerText . toLowerCase ( ) . includes ( keyword ) ) ;
72
- } ) ;
73
-
74
- foundLinks . forEach ( link => {
75
- const linkDomain = rootDomain ( new URL ( link . href , window . location . origin ) . hostname ) ;
76
- if ( linkDomain === currentDomain ) {
77
- if ( ! linksMap [ keyword ] ) {
78
- linksMap [ keyword ] = { href : link . href , text : link . innerText . trim ( ) } ;
79
-
80
- if ( ! summarizedLinks [ link . href ] && link . innerText . trim ( ) !== "" ) {
81
- const summaryRequestData = {
82
- action : "generateSummary" ,
83
- url : link . href ,
84
- policyName : link . innerText . trim ( )
85
- } ;
86
- chrome . runtime . sendMessage ( summaryRequestData ) ;
87
- displayPreloader ( link . innerText . trim ( ) , currentDomain ) ;
88
- console . log ( "Sent summary request for " + link . innerText . trim ( ) ) ;
89
- summarizedLinks [ link . href ] = true ;
90
- localStorage . setItem ( 'summarizedLinks' , JSON . stringify ( summarizedLinks ) ) ;
91
- }
92
- }
67
+ console . log ( "Auto summaries: " + autoSummaries ) ;
68
+ keywords . forEach ( keyword => {
69
+ const foundLinks = Array . from ( document . querySelectorAll ( 'a' ) ) . filter ( link => {
70
+ return ( link . href . toLowerCase ( ) . includes ( keyword ) || link . innerText . toLowerCase ( ) . includes ( keyword ) ) ;
71
+ } ) ;
72
+
73
+ foundLinks . forEach ( link => {
74
+ const linkDomain = rootDomain ( new URL ( link . href , window . location . origin ) . hostname ) ;
75
+ if ( linkDomain === currentDomain ) {
76
+ if ( ! summarizedLinks [ link . href ] && link . innerText . trim ( ) !== "" && autoSummaries ) {
77
+ const summaryRequestData = {
78
+ action : "generateSummary" ,
79
+ url : link . href ,
80
+ policyName : link . innerText . trim ( )
81
+ } ;
82
+ chrome . runtime . sendMessage ( summaryRequestData ) ;
83
+ displayPreloader ( link . innerText . trim ( ) , currentDomain ) ;
84
+ console . log ( "Sent summary request for " + link . innerText . trim ( ) ) ;
85
+ summarizedLinks [ link . href ] = true ;
86
+ localStorage . setItem ( 'summarizedLinks' , JSON . stringify ( summarizedLinks ) ) ;
93
87
}
94
- } ) ;
88
+ }
95
89
} ) ;
96
- }
90
+ } ) ;
97
91
} ) ;
98
-
99
-
100
- if ( Object . keys ( linksMap ) . length > 0 ) {
101
- return Object . values ( linksMap ) ;
102
- } else {
103
- return [ ] ;
104
- }
105
92
}
106
93
107
- chrome . runtime . onMessage . addListener ( function ( request , sender , sendResponse ) {
94
+
95
+ chrome . runtime . onMessage . addListener ( function ( request , sender , sendResponse ) {
108
96
if ( request . action === "findLinks" ) {
109
- const links = handlePolicyLinks ( ) ;
110
- sendResponse ( { links : links } ) ;
111
- return true ;
97
+ const keywords = [ 'privacy' , 'term' , 'return' , 'shipping' , 'legal' , 'cookie' ] ; // Add more keywords as needed
98
+ let linksMap = { } ;
99
+ const currentDomain = rootDomain ( new URL ( window . location . href ) . hostname ) ;
100
+
101
+ // Helper function to check if domains match considering subdomains
102
+ const isDomainMatch = ( linkDomain , currentDomain ) => {
103
+ return linkDomain . includes ( currentDomain ) || currentDomain . includes ( linkDomain ) ;
104
+ } ;
105
+
106
+ keywords . forEach ( keyword => {
107
+ const foundLinks = Array . from ( document . querySelectorAll ( 'a' ) ) . filter ( link => {
108
+ return ( link . href . toLowerCase ( ) . includes ( keyword ) || link . innerText . toLowerCase ( ) . includes ( keyword ) ) ;
109
+ } ) ;
110
+
111
+ for ( let i = foundLinks . length - 1 ; i >= 0 ; i -- ) {
112
+ const linkDomain = rootDomain ( new URL ( foundLinks [ i ] . href , window . location . origin ) . hostname ) ;
113
+ if ( isDomainMatch ( linkDomain , currentDomain ) ) {
114
+ linksMap [ keyword ] = { href : foundLinks [ i ] . href , text : foundLinks [ i ] . innerText . trim ( ) } ;
115
+ break ;
116
+ }
117
+ }
118
+ } ) ;
119
+
120
+ let links = Object . keys ( linksMap ) . map ( keyword => ( {
121
+ keyword : keyword ,
122
+ href : linksMap [ keyword ] . href ,
123
+ text : linksMap [ keyword ] . text
124
+ } ) ) ;
125
+ sendResponse ( { links : links } ) ;
112
126
}
113
127
} ) ;
114
128
129
+
130
+
131
+
115
132
detectCheckboxes ( ) ;
116
133
detectTextConsent ( ) ;
117
134
handlePolicyLinks ( ) ;
0 commit comments