PageSourceSearch

https://ra-publications.us/wp-content/plugins/local-links/public/replacelinks.js?ver=1791394677

js ra-publications.us collected 2026-10-07 17:38:36 UTC 15,087 bytes, 424 lines download raw bytes

1/*
2Local Links is free software: you can redistribute it and/or modify
3it under the terms of the GNU General Public License as published by
4the Free Software Foundation, either version 2 of the License, or
5any later version.
6
7Local Links is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY; without even the implied warranty of
9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10GNU General Public License for more details.
11
12You should have received a copy of the GNU General Public License
13along with Local Links. If not, see https://www.gnu.org/licenses/gpl-2.0.html.
14*/
15
16/*
17JavaScript to rewrite links
18locallinks_opts array is passed from PHP. Elements:
19	'amz' => Amazon (0/1)
20	'apl' => Apple (0/1)
21	'kobo' => Kobo (0/1)
22	'alib' => Alibris (0/1)
23	'google' => Google (0/1)
24	'abe' => AbeBooks (0/1)
25	'bsorg' => Bookshop.org (0/1)
26	'obs' => OneBookShelf (0/1)
27	'sw' => Smashwords (0/1)
28
29	'amztld' => Amazon TLD
30	'aplcc' => Apple CC for Apple Books
31	'alibtld' => Alibris TLD
32	'abedomain' => AbeBooks domain
33	'bsorgdomain' = >Bookshop.org domain
34
35	'amzaff' => Amazon affiliate code for the current country
36	'aplaff' => Apple affiliate code
37	'googleaff' => Google affiliate code
38	'koboid' => Kobo affiliate id
39	'kobomid' => Kobo affiliate mid
40	'bsorgaff' => Bookshop.org affiliate code for the current country
41	'obsaff' => OneBookShelf affiliate code
42	'obssrc' => OneBookShelf src parameter
43	'swaff' => Smashwords affiliate code
44*/
45
46jQuery(document).ready(function() {
47	console.log ('Local Links CC: ' + locallinks_opts['locallinks_cc'])
48	const { __, _x, _n, sprintf } = wp.i18n
49
50	// Regular expressions to find relevant links
51	locallinks_reAmazon = new RegExp (/https?:\/\/(smile\.|w{3}\.)?amazon\.[^/]+.*/i)
52	locallinks_reApple = new RegExp (/https?:\/\/.*itunes\.apple\.com.*/i)
53	locallinks_reAppleBooks = new RegExp (/https?:\/\/books\.apple\.com.*/i)
54	locallinks_reKobo = new RegExp (/https?:\/\/(w{3}\.)?kobo(books)?\.com\/[a-z]{2}\/[a-z]{2}.*/i)
55	locallinks_reKobo = new RegExp (/https?:\/\/(w{3}\.)?kobo(books)?\.com.*/i)
56	locallinks_reAlibris = new RegExp (/https?:\/\/(w{3}.)?alibris\./i)
57	locallinks_reGoogle = new RegExp (/https?:\/\/(w{3}.)?play\.google\.com/i)
58	locallinks_reAbe = new RegExp (/https?:\/\/(w{3}.)?abebooks\.[^/]+.*/i)
59	locallinks_reBN = new RegExp (/https?:\/\/(w{3}.)?barnesandnoble\.com/i)
60	locallinks_reBSorg = new RegExp (/https?:\/\/([a-z]{2}\.)?bookshop\.org/i)
61	locallinks_reOBS = new RegExp (/https?:\/\/(w{3}\.)(drivethru[a-z]{3,7}|wargamevault)\.com.*/i)
62	locallinks_reSW = new RegExp (/https?:\/\/(w{3}\.)smashwords\.com.*/i)
63
64	// Loop through links
65	jQuery('a').each(function() {
66
67		// Initialise variables
68		locallinks_url = ''
69		locallinks_aff = false
70		locallinks_title = ''
71
72		// Get the URL
73		try {
74			locallinks_url = new URL(jQuery(this).attr('href'))
75			if (jQuery(this)[0].hasAttribute ('title'))
76				locallinks_title = jQuery(this).attr('title')
77
78			// Remove Facebook fbclid GET parameter on all links
79			if (locallinks_url.searchParams.has('fbclid')) {
80				locallinks_url.searchParams.delete('fbclid')
81				jQuery(this).attr('href', locallinks_url.toString())
82			}
83
84			// Remove Google gclid GET parameter on all links
85			if (locallinks_url.searchParams.has('gclid')) {
86				locallinks_url.searchParams.delete('gclid')
87				jQuery(this).attr('href', locallinks_url.toString())
88			}
89
90			// Test for an Amazon link
91			if (locallinks_opts['amz'] == '1') {
92				if (locallinks_reAmazon.test (jQuery(this).attr('href')) == true) {
93					// Set default title if none is set
94					if (locallinks_title == '')
95						locallinks_title = __('Buy on Amazon', 'local-links')
96
97					// Check if it is an author page link
98					if (jQuery(this).attr('href').search ('/author/') != -1) {
99						// Author link. Just replace country code and strip GET parameters
100						locallinks_sAmazon = jQuery(this).attr('href').replace(/(\/.*amazon.)[a-zA-Z.]+\/([^?]+).*/, '$1' + locallinks_opts['amztld'] + '/$2')
101					}
102					else {
103						// Not an author link. Build new URL
104						locallinks_sAmazon = 'https://www.amazon.' +
105							locallinks_opts['amztld'] +
106							locallinks_url.pathname.replace(/\/.*\/([A-Z0-9]{10}).*$/i, '/dp/$1')
107					}
108
109					// Add Amazon affiliate code if one has been set for this country
110					if (locallinks_opts['amzaff'] != '') {
111						if (/\?/.test(locallinks_sAmazon) === false)
112							locallinks_sAmazon += '?'
113						else
114							locallinks_sAmazon += '&'
115						locallinks_sAmazon += 'tag=' + locallinks_opts['amzaff']
116						locallinks_aff = true
117					}
118
119					// If Amazon attribution tagging was included in original URL, re-add it
120					var locallinks_params = new URLSearchParams(locallinks_url.
120search)
121					if (locallinks_params.has ('maas'))
122						locallinks_maas = locallinks_params.get('maas')
123					else
124						locallinks_maas = ''
125					if (locallinks_params.has ('ref_'))
126						locallinks_ref = locallinks_params.get('ref_')
127					else
128						locallinks_ref = ''
129					if (locallinks_maas != '' && locallinks_ref != '') {
130						if (/\?/.test(locallinks_sAmazon) === false)
131							locallinks_sAmazon += '?'
132						else
133							locallinks_sAmazon += '&'
134						locallinks_sAmazon += 'maas=' + locallinks_maas + '&ref_=' + locallinks_ref
135					}
136
137					// Replace the link's href attribute
138					jQuery(this).attr('href', locallinks_sAmazon)
139				}
140			}
141
142			// Test for an Apple link
143			if (locallinks_opts['apl'] == '1') {
144				// iTunes
145				if (locallinks_reApple.test (jQuery(this).attr('href')) == true) {
146					// Set default title if none is set
147					if (locallinks_title == '')
148						locallinks_title = __('Buy at Apple', 'local-links')
149
150					// Build new URL
151					locallinks_sApple = 'https://geo.itunes.apple.com' +
152						locallinks_url.pathname
153
154					// Add Apple affiliate code if one has been set
155					if (locallinks_opts['aplaff'] != '') {
156						if (/\?/.test(locallinks_sApple) === false)
157							locallinks_sApple += '?'
158						else
159							locallinks_sApple += '&'
160						locallinks_sApple += 'at=' + locallinks_opts['aplaff']
161						locallinks_aff = true
162					}
163
164					// Replace the link's href attribute
165					jQuery(this).attr('href', locallinks_sApple)
166				}
167
168				// Apple Books
169				if (locallinks_reAppleBooks.test (jQuery(this).attr('href')) == true) {
170					// Set default title if none is set
171					if (locallinks_title == '')
172						locallinks_title = __('Buy at Apple Books', 'local-links')
173
174					// Set up path, starting with the country code
175					locallinks_sApplePath = '/' + locallinks_opts['aplcc']
176					// Add the rest of the path
177					locallinks_sApplePath += locallinks_url.pathname.replace(/\/[a-z][a-z]\/(audio)?book\//i, '/$1book/')
178					// Build new URL
179					locallinks_sApple = 'https://books.apple.com' +
180						locallinks_sApplePath
181
182					// Add Apple affiliate code if one has been set
183					if (locallinks_opts['aplaff'] != '') {
184						if (/\?/.test(locallinks_sApple) === false)
185							locallinks_sApple += '?'
186						else
187							locallinks_sApple += '&'
188						locallinks_sApple += 'at=' + locallinks_opts['aplaff']
189						locallinks_aff = true
190					}
191
192					// Replace the link's href attribute
193					jQuery(this).attr('href', locallinks_sApple)
194				}
195			}
196
197			// Test for a Kobo link
198			if (locallinks_opts['kobo'] == '1') {
199				if (locallinks_reKobo.test (jQuery(this).attr('href')) == true) {
200					// Set default title if none is set
201					if (locallinks_title == '')
202						locallinks_title = __('Buy at Kobo', 'local-links')
203
204					// Remove country & language from pathname
205					locallinks_kobopath = locallinks_url.pathname.replace(/\/[a-z]{2}\/[a-z]{2}/i, '')
206					// Build URL
207					locallinks_sKobo = 'https://www.kobo.com' +
208						locallinks_kobopath +
209						locallinks_url.search
210					// Set up affiliate link if id and mid set
211					if (locallinks_opts['koboid'] != '' && locallinks_opts['kobomid'] != '') {
212						locallinks_sKobo = 'https://click.linksynergy.com/deeplink?murl=' +
213							encodeURIComponent (locallinks_sKobo) +
214							'&id=' + locallinks_opts['koboid'] +
215							'&mid=' + locallinks_opts['kobomid']
216						locallinks_aff = true
217					}
218
219					// Replace the link's href attribute
220					jQuery(this).attr('href', locallinks_sKobo)
221				}
222			}
223
224			// Test for an Alibris link
225			if (locallinks_opts['alib'] == '1') {
226				if (locallinks_reAlibris.test (jQuery(this).attr('href')) == true) {
227					// Set default title if none is set
228					if (locallinks_title == '')
229						locallinks_title = __('Buy at Alibris', 'local-links')
230
231					// Build new URL
232					locallinks_sAlibris = 'https://www.alibris.' +
233						locallinks_opts['alibtld'] +
234						locallinks_url.pathname
235
236					// Replace the link's href attribute
237					jQuery(this).attr('href', locallinks_sAlibris)
238				}
239			}
240
241			// Test for a Google link
242			if (locallinks_opts['google'] == '1') {
243				if (locallinks_reGoogle.test (jQuery(this).attr('href')) == true) {
244					// Set default title if none is set
245					if (locallinks_title == '')
246						locallinks_title = __('Buy at Google', 'local-links')
247
248					// Build new URL
249					locallinks_sGoogle = locallinks_url.protocol + '//' +
250						locallinks_url.hostname +
251						locallinks_url.pathname +
252						'?id=' +
253						locallinks_url.searchParams.get('id')
254
255					// Add Google affiliate code if one has been set
256					if (locallinks_opts['googleaff'] != '') {
257						if (/\?/.test(locallinks_sGoogle) === false)
258							locallinks_sGoogle += '?'
259						else
260							locallinks_sGoogle += '&'
261						locallinks_sGoogle += 'PAffiliateID=' + locallinks_opts['googleaff']
262						locallinks_aff = true
263					}
264
265					// Replace the link's href attribute
266					jQuery(this).attr('href', locallinks_sGoogle)
267				}
268			}
269
270			// Test for an AbeBooks link
271			if (locallinks_opts['abe'] == '1') {
272				if (locallinks_reAbe.test (jQuery(this).attr('href')) == true) {
273					// Set default title if none is set
274					if (locallinks_title == '')
275						locallinks_title = __('Buy at AbeBooks', 'local-links')
276
277					// Build new URL
278					locallinks_sAbe = locallinks_url.protocol + '//' +
279						locallinks_opts['abedomain'] +
280						locallinks_url.pathname
281					if (locallinks_url.pathname.substring(1, 8) == 'servlet')
282						locallinks_sAbe += '?bi=' + locallinks_url.searchParams.get('bi')
283					// Replace the link's href attribute
284					jQuery(this).attr('href', locallinks_sAbe)
285				}
286			}
287
288			// Test for a Barnes & Noble link. Have to work on full URL, not just pathname, because B&N use extra parameters to separate editions
289			if (locallinks_reBN.test (jQuery(this).attr('href')) == true) {
290				// Set default title if none is set
291				if (locallinks_title == '')
292					locallinks_title = __('Buy at Barnes and Noble', 'local-links')
293
294				// Remove jsessionid
295				locallinks_sBN = jQuery(this).attr('href').replace(/;jsessionid=.*/i, '')
296				// Replace the link's href attribute
297				jQuery(this).attr('href', locallinks_sBN)
298			}
299
300			// Test for a Bookshop.org link
301			if (locallinks_opts['bsorg'] == '1') {
302				if (locallinks_reBSorg.test (jQuery(this).attr('href')) == true) {
303					// Set default title if none is set
304					if (locallinks_title == '')
305						locallinks_title = __('Buy at Bookshop.org', 'local-links')
306
307					// Initialise replacement link
308					locallinks_sBSorg = 'https://' + locallinks_opts['bsorgdomain']
309
310					// Work out what type of link it is
311					if (locallinks_url.pathname.substring(2, 9) == '/books/') {
312						// Book link. Get ISBN
313						isbn = locallinks_url.searchParams.get('ean')
314						if (locallinks_opts['bsorgaff'] == '') {
315							// No affiliate ID. Just replace the domain
316							locallinks_sBSorg += locallinks_url.pathname
317						}
318						else {
319							// Create affiliate link
320							locallinks_sBSorg += '/a/' + locallinks_opts['bsorgaff'] + '/' + isbn
321							locallinks_aff = true
322						}
323					}
324					else if (locallinks_url.pathname.substring(0, 3) == '/a/') {
325						// Affiliate link. Get ISBN
326						isbn = locallinks_url.pathname.replace(/.*([0-9]{13}).*/, '$1')
327						if (locallinks_opts['bsorgaff'] == '') {
328							// No affiliate ID. Create search link
329							locallinks_sBSorg += '/books?keywords=' + isbn
330						}
331						else {
332							// Create affiliate link
333							locallinks_sBSorg += '/a/' + locallinks_opts['bsorgaff'] + '/' + isbn
334							locallinks_aff = true
335						}
336					}
337					else if (locallinks_url.pathname.substring(0, 7) == '/lists/') {
338						// Lists are country-specific, so just go to the link
339						locallinks_sBSorg = locallinks_url.href
340					}
341					else if (locallinks_url.pathname.substring(0, 6) == '/shop/') {
342						// Shops are country-specific, so just go to the link
343						locallinks_sBSorg = locallinks_url.href
344					}
345					else {
346						// Store locator, home page, etc. Just replace domain
347						locallinks_sBSorg += locallinks_url.pathname
348					}
349
350					// Replace the link's href attribute
351					jQuery(this).attr('href', locallinks_sBSorg)
352				}
353			}
354
355			// Test for a OneBookShelf link
356			if (locallinks_opts['obs'] == '1') {
357				if (locallinks_reOBS.test (jQuery(this).attr('href')) == true) {
358					// Set default title if none is set
359					if (locallinks_title == '')
360						locallinks_title = __('Buy at OneBookShelf', 'local-links')
361
362					// Initialise URL and query
363					locallinks_sOBS = locallinks_url.origin + locallinks_url.pathname
364					locallinks_obsquery = '?'
365
366					if (locallinks_opts['obsaff'] != '') {
367						locallinks_obsquery += 'affiliate_id=' + encodeURIComponent(locallinks_opts['obsaff'])
368						locallinks_aff = true
369					}
370					if (locallinks_opts['obsaff'] != '' && locallinks_opts['obssrc'] != '')
371						locallinks_obsquery += '&'
372					if (locallinks_opts['obssrc'] != '')
373						locallinks_obsquery += 'src=' + encodeURIComponent(locallinks_opts['obssrc'])
374
375					// Append obsquery if it has been set
376					if (locallinks_obsquery != '?')
377						locallinks_sOBS += locallinks_obsquery
378					// Replace the link's href attribute
379					jQuery(this).attr('href', locallinks_sOBS)
380				}
381			}
382
383			// Test for a Smashwords link
384			if (locallinks_opts['sw'] == '1') {
385				if (locallinks_reSW.test (jQuery(this).attr('href')) == true) {
386					// Set default title if none is set
387					if (locallinks_title == '')
388						locallinks_title = __('Buy at Smashwords', 'local-links')
389
390					// Initialise URL and query
391					locallinks_sSW = locallinks_url.origin + locallinks_url.pathname
392					locallinks_swquery = ''
393
394					if (locallinks_opts['swaff'] != '') {
395						locallinks_swquery = '?ref=' + encodeURIComponent(locallinks_opts['swaff'])
396						locallinks_aff = true
397					}
398
399					// Replace the link's href attribute
400					locallinks_sSW += locallinks_swquery
401					jQuery(this).attr('href', locallinks_sSW)
402				}
403			}
404
405			// Replace the link's title attribute
406			if (locallinks_aff) {
407				if (locallinks_title != '')
408					locallinks_title += __(' (affiliate link)', 'local-links')
409				else
410					locallinks_title = __('Affiliate link', 'local-links')
411			}
412			if (locallinks_title != '')
413				jQuery(this).attr('title', locallinks_title)
414		}
415		catch (err) {
416			// Uncomment next line for debugging
417			// console.log (err)
418		}
419		finally {
420			// Ensure locallinks_url is empty
421			locallinks_url = ''
422		}
423	})
424})

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.