-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
57 lines (46 loc) · 1.85 KB
/
content.js
File metadata and controls
57 lines (46 loc) · 1.85 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
const version = detectDrupalVersion();
if (version) {
chrome.runtime.sendMessage({ type: 'DRUPAL_VERSION_FOUND', version });
window.detectDrupalVersion = () => version;
} else {
window.detectDrupalVersion = () => null;
}
function detectDrupalVersion() {
const domVersionEl = document.querySelector('.field--name-field-issue-version .field__item');
if (domVersionEl) {
const domText = domVersionEl.textContent.trim();
const domMatch = domText.match(/^(\d+\.\d+(?:\.\d+)?)/);
if (domMatch) return domMatch[1];
}
const meta = document.querySelector('meta[name="Generator"]');
if (meta) {
const content = meta.getAttribute('content');
const metaMatch = content.match(/Drupal\s+(\d+(?:\.\d+)?)/);
if (metaMatch) return metaMatch[1];
}
// Filtered ?v= version check
const versionRegex = /[?&]v=(\d+\.\d+\.\d+)/;
const assetTags = document.querySelectorAll('script[src], link[href]');
const versions = new Set();
for (const el of assetTags) {
const src = el.getAttribute('src') || el.getAttribute('href');
if (!src || !versionRegex.test(src)) continue;
const url = new URL(src, window.location.origin);
// Only accept local or Drupal-related paths
const isLocal = url.origin === window.location.origin;
const isDrupalish = url.pathname.includes('/core/') ||
url.pathname.includes('/modules/') ||
url.pathname.includes('/themes/') ||
url.pathname.includes('/libraries/');
if (isLocal || isDrupalish) {
const match = src.match(versionRegex);
if (match) versions.add(match[1]);
}
}
const sortedVersions = [...versions].sort((a, b) => {
const va = a.split('.').map(Number);
const vb = b.split('.').map(Number);
return vb[0] - va[0] || vb[1] - va[1] || vb[2] - va[2];
});
return sortedVersions[0] || null;
}