-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
140 lines (116 loc) · 4.51 KB
/
Copy pathcontentScript.js
File metadata and controls
140 lines (116 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// contentScript.js
// Mapping of taxonomy IDs to species names
const taxonomyMap = {
"9615": "Canine",
"9606": "Human",
"9913": "Cattle",
"10090": "Mouse",
"7955": "Zebrafish",
"3702": "Arabidopsis",
};
// Base URL for Snipe search
const SNIPE_BASE_URL = 'https://snipe-bio.github.io/explore/';
// Function to create the "Explore with Snipe" button with specific text
function createSnipeButton(params, type) {
console.log('Creating Snipe Button with params:', params, 'and type:', type);
const button = document.createElement('a');
button.className = 'snipe-button'; // Assign the button class
// Determine button text based on type
let buttonText = '';
switch (type) {
case 'bioproject':
buttonText = 'Explore Bioproject with Snipe';
break;
case 'biosample':
buttonText = 'Explore BioSample with Snipe';
break;
case 'experiment':
buttonText = 'Explore Experiment with Snipe';
break;
case 'species':
buttonText = 'Explore Species with Snipe';
break;
default:
buttonText = 'Explore with Snipe';
}
button.textContent = buttonText; // Button text
// Accessibility attributes
button.setAttribute('role', 'button');
button.setAttribute('aria-label', buttonText);
// Construct the URL with query parameters
const url = new URL(SNIPE_BASE_URL);
for (const key in params) {
if (params[key]) {
url.searchParams.append(key, params[key]);
}
}
console.log('Constructed Snipe URL:', url.toString());
// Set the href and target attributes
button.href = url.toString();
button.target = '_blank';
button.rel = 'noopener noreferrer';
return button;
}
// Function to highlight and add the button to an element
function highlightElement(element, params, type) {
console.log('Highlighting element:', element, 'with params:', params, 'and type:', type);
if (!element) return;
// Add highlight class
element.classList.add('snipe-highlight');
// Create button container
const buttonContainer = document.createElement('span');
buttonContainer.className = 'snipe-button-container';
// Create and append the button
const button = createSnipeButton(params, type);
buttonContainer.appendChild(button);
// Append the button container after the element
element.parentElement.appendChild(buttonContainer);
}
// Main function to process the page
function processPage() {
console.log('Processing page...');
// Get the Experiment ID
const experimentLink = document.querySelector('a[href^="/sra/SRX"], a[href^="/sra/ERX"], a[href^="/sra/DRX"]');
const experimentID = experimentLink ? experimentLink.textContent.trim() : null;
// Get the BioProject
const bioprojectLink = document.querySelector('a[href^="/bioproject/"]');
const bioprojectID = bioprojectLink ? bioprojectLink.textContent.trim() : null;
// Get the BioSample
const biosampleLink = document.querySelector('a[href^="/biosample/"]');
const biosampleID = biosampleLink ? biosampleLink.textContent.trim() : null;
// Get the Species
const speciesLink = document.querySelector('a[href^="/Taxonomy/Browser/wwwtax.cgi"]');
let species = null;
if (speciesLink) {
const taxIDMatch = speciesLink.href.match(/id=(\d+)/);
if (taxIDMatch) {
const taxID = taxIDMatch[1];
species = taxonomyMap[taxID];
}
}
console.log('Found species:', species);
// Only proceed if species is supported
if (!species) {
console.log('Species not supported. Exiting.');
return; // Do not display any buttons if species is not supported
}
// Highlight and add buttons for supported species
if (experimentLink && experimentID) {
highlightElement(experimentLink, { 'sra-exp': experimentID, species }, 'experiment');
}
if (bioprojectLink && bioprojectID) {
highlightElement(bioprojectLink, { 'bioproject': bioprojectID, species }, 'bioproject');
}
if (biosampleLink && biosampleID) {
highlightElement(biosampleLink, { 'biosample': biosampleID, species }, 'biosample');
}
if (speciesLink && species) {
highlightElement(speciesLink, { 'species': species }, 'species');
}
}
// Run the main function when the DOM is fully loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', processPage);
} else {
processPage();
}