2024-11-28 17:43:40 +03:00
|
|
|
window.router = new class {
|
|
|
|
get csrf() {
|
|
|
|
return u("meta[name=csrf]").attr("value")
|
|
|
|
}
|
|
|
|
|
|
|
|
__isScriptAlreadyLoaded(script) {
|
|
|
|
if(script.src) {
|
|
|
|
const script_url = new URL(script.src)
|
|
|
|
const script_main_part = script_url.pathname
|
2024-12-01 18:12:50 +03:00
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
return u(`script[src^='${script_main_part}']`).length > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
__appendScript(script) {
|
|
|
|
const _t_scr = document.createElement('script')
|
2024-11-28 23:15:50 +03:00
|
|
|
_t_scr.crossorigin = 'anonymous'
|
|
|
|
if(script.getAttribute('integrity')) {
|
|
|
|
_t_scr.setAttribute('integrity', script.getAttribute('integrity'))
|
|
|
|
}
|
|
|
|
|
2024-11-29 23:26:20 +03:00
|
|
|
if(script.getAttribute('id')) {
|
|
|
|
_t_scr.id = script.id
|
|
|
|
}
|
|
|
|
|
|
|
|
if(script.getAttribute('type')) {
|
|
|
|
_t_scr.type = script.type
|
|
|
|
}
|
|
|
|
|
2024-11-28 23:15:50 +03:00
|
|
|
//const parent = script.parentNode
|
|
|
|
//const idx = Array.from(parent.children).indexOf(script)
|
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
if(script.src) {
|
|
|
|
_t_scr.src = script.src
|
|
|
|
} else {
|
2024-11-29 23:26:20 +03:00
|
|
|
_t_scr.async = false
|
2024-11-28 17:43:40 +03:00
|
|
|
_t_scr.textContent = script.textContent
|
|
|
|
}
|
|
|
|
|
2024-11-28 23:15:50 +03:00
|
|
|
//parent.children[idx].before(script)
|
2024-11-28 17:43:40 +03:00
|
|
|
document.body.appendChild(_t_scr)
|
|
|
|
}
|
|
|
|
|
|
|
|
__clearScripts() {
|
|
|
|
u(`script:not([src])`).remove()
|
|
|
|
}
|
|
|
|
|
2024-11-28 23:15:50 +03:00
|
|
|
__closeMsgs() {
|
2024-11-30 15:35:43 +03:00
|
|
|
window.messagebox_stack.forEach(msg => {
|
|
|
|
if(msg.hidden) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.close()
|
|
|
|
})
|
2024-11-28 23:15:50 +03:00
|
|
|
}
|
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
__appendPage(parsed_content) {
|
2024-11-29 23:26:20 +03:00
|
|
|
const scripts_to_append = []
|
2024-11-28 17:43:40 +03:00
|
|
|
const page_body = u(parsed_content.querySelector('.page_body'))
|
|
|
|
const sidebar = u(parsed_content.querySelector('.sidebar'))
|
|
|
|
const page_header = u(parsed_content.querySelector('.page_header'))
|
2024-12-01 18:12:50 +03:00
|
|
|
const backdrop = u(parsed_content.querySelector('#backdrop'))
|
2024-11-28 17:43:40 +03:00
|
|
|
if(page_body.length < 1) {
|
2024-11-29 23:26:20 +03:00
|
|
|
throw new Error('Invalid page has been loaded')
|
2024-11-28 17:43:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-29 23:26:20 +03:00
|
|
|
window.__current_page_audio_context = null
|
2024-11-28 17:43:40 +03:00
|
|
|
this.__clearScripts()
|
2024-11-28 23:15:50 +03:00
|
|
|
parsed_content.querySelectorAll('.page_body script, #_js_ep_script').forEach(script => {
|
2024-11-28 17:43:40 +03:00
|
|
|
if(!this.__isScriptAlreadyLoaded(script)) {
|
2024-11-29 23:26:20 +03:00
|
|
|
scripts_to_append.push(script)
|
2024-11-28 23:15:50 +03:00
|
|
|
script.parentNode.removeChild(script)
|
2024-11-28 17:43:40 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
u('.page_body').html(page_body.html())
|
|
|
|
u('.sidebar').html(sidebar.html())
|
2024-12-01 18:12:50 +03:00
|
|
|
if(backdrop.length > 0) {
|
|
|
|
if(u('#backdrop').length == 0) {
|
|
|
|
u('body').append(`<div id="backdrop"></div>`)
|
|
|
|
}
|
|
|
|
u('#backdrop').nodes[0].outerHTML = (backdrop.nodes[0].outerHTML)
|
|
|
|
} else {
|
|
|
|
u('#backdrop').remove()
|
|
|
|
}
|
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
if(u('.page_header #search_box select').length > 0 && page_header.find('#search_box select').length > 0) {
|
|
|
|
u('.page_header #search_box select').nodes[0].value = page_header.find('#search_box select').nodes[0].value
|
|
|
|
}
|
|
|
|
|
|
|
|
if(page_header.hasClass('search_expanded_at_all')) {
|
|
|
|
u('.page_header').addClass('search_expanded_at_all').addClass('search_expanded')
|
|
|
|
} else {
|
|
|
|
if(u('.page_header').hasClass('search_expanded_at_all')) {
|
|
|
|
u('.page_header').removeClass('search_expanded_at_all').removeClass('search_expanded')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u("meta[name=csrf]").attr("value", u(parsed_content.querySelector('meta[name=csrf]')).attr('value'))
|
|
|
|
|
|
|
|
document.title = parsed_content.title
|
2024-11-29 23:26:20 +03:00
|
|
|
scripts_to_append.forEach(append_me => {
|
|
|
|
this.__appendScript(append_me)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async __integratePage() {
|
2024-11-28 17:43:40 +03:00
|
|
|
window.scrollTo(0, 0)
|
|
|
|
bsdnHydrate()
|
|
|
|
|
|
|
|
if(u('.paginator:not(.paginator-at-top)').length > 0) {
|
|
|
|
showMoreObserver.observe(u('.paginator:not(.paginator-at-top)').nodes[0])
|
|
|
|
}
|
2024-11-28 23:15:50 +03:00
|
|
|
|
2024-11-29 23:26:20 +03:00
|
|
|
if(u(`div[class$="_small_block"]`).length > 0 && typeof smallBlockObserver != 'undefined') {
|
2024-11-28 23:15:50 +03:00
|
|
|
smallBlockObserver.observe(u(`div[class$="_small_block"]`).nodes[0])
|
|
|
|
}
|
2024-11-29 23:26:20 +03:00
|
|
|
|
|
|
|
if(window.player) {
|
|
|
|
await window.player._handlePageTransition()
|
|
|
|
}
|
2024-11-28 23:15:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
__unlinkObservers() {
|
|
|
|
if(u('.paginator:not(.paginator-at-top)').length > 0) {
|
|
|
|
showMoreObserver.unobserve(u('.paginator:not(.paginator-at-top)').nodes[0])
|
|
|
|
}
|
|
|
|
|
2024-11-29 23:26:20 +03:00
|
|
|
if(u(`div[class$="_small_block"]`).length > 0 && typeof smallBlockObserver != 'undefined') {
|
2024-11-28 23:15:50 +03:00
|
|
|
smallBlockObserver.unobserve(u(`div[class$="_small_block"]`).nodes[0])
|
|
|
|
}
|
2024-11-28 17:43:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
checkUrl(url) {
|
2024-11-29 23:26:20 +03:00
|
|
|
if(window.openvk.disable_ajax == 1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
if((localStorage.getItem('ux.disable_ajax_routing') ?? 0) == 1 || window.openvk.current_id == 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!url || url == '') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if(url.indexOf(location.origin) == -1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-11-28 23:15:50 +03:00
|
|
|
if(url.indexOf('hash=') != -1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
async route(params = {}) {
|
|
|
|
if(typeof params == 'string') {
|
|
|
|
params = {
|
|
|
|
url: params
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const old_url = location.href
|
|
|
|
let url = params.url
|
|
|
|
if(url.indexOf(location.origin)) {
|
|
|
|
url = location.origin + url
|
|
|
|
}
|
|
|
|
|
|
|
|
if((localStorage.getItem('ux.disable_ajax_routing') ?? 0) == 1 || window.openvk.current_id == 0) {
|
|
|
|
window.location.assign(url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const push_url = params.push_state ?? true
|
|
|
|
const next_page_url = new URL(url)
|
|
|
|
if(push_url) {
|
|
|
|
history.pushState({'from_router': 1}, '', url)
|
|
|
|
} else {
|
|
|
|
history.replaceState({'from_router': 1}, '', url)
|
|
|
|
}
|
|
|
|
|
|
|
|
const parser = new DOMParser
|
|
|
|
const next_page_request = await fetch(next_page_url, {
|
2024-12-01 21:53:15 +03:00
|
|
|
method: 'AJAX',
|
|
|
|
referrer: old_url,
|
2024-11-29 23:26:20 +03:00
|
|
|
headers: {
|
|
|
|
'X-OpenVK-Ajax-Query': '1',
|
|
|
|
}
|
2024-11-28 17:43:40 +03:00
|
|
|
})
|
|
|
|
const next_page_text = await next_page_request.text()
|
|
|
|
const parsed_content = parser.parseFromString(next_page_text, 'text/html')
|
|
|
|
if(next_page_request.redirected) {
|
|
|
|
history.replaceState({'from_router': 1}, '', next_page_request.url)
|
|
|
|
}
|
|
|
|
|
2024-11-28 23:15:50 +03:00
|
|
|
this.__closeMsgs()
|
|
|
|
this.__unlinkObservers()
|
2024-11-29 23:26:20 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.__appendPage(parsed_content)
|
|
|
|
await this.__integratePage()
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e)
|
|
|
|
next_page_url.searchParams.delete('al', 1)
|
|
|
|
location.assign(next_page_url)
|
|
|
|
}
|
2024-11-28 17:43:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u(document).on('click', 'a', async (e) => {
|
|
|
|
const target = u(e.target).closest('a')
|
|
|
|
const dom_url = target.attr('href')
|
|
|
|
const id = target.attr('id')
|
|
|
|
let url = target.nodes[0].href
|
|
|
|
|
|
|
|
if(id) {
|
2024-11-29 23:26:20 +03:00
|
|
|
if(['act_tab_a', 'ki', 'used', '_pinGroup', 'profile_link'].indexOf(id) == -1) {
|
2024-11-28 17:43:40 +03:00
|
|
|
console.log('AJAX | Skipping cuz maybe its function call link.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 23:26:20 +03:00
|
|
|
/*if(url.indexOf('hash=') != -1) {
|
2024-11-28 23:15:50 +03:00
|
|
|
e.preventDefault()
|
|
|
|
return false
|
2024-11-29 23:26:20 +03:00
|
|
|
}*/
|
|
|
|
|
2024-12-01 21:53:15 +03:00
|
|
|
if(target.attr('rel') == 'nofollow') {
|
2024-11-29 23:26:20 +03:00
|
|
|
console.log('AJAX | Skipped because its nofollow')
|
|
|
|
return
|
2024-11-28 23:15:50 +03:00
|
|
|
}
|
|
|
|
|
2024-11-30 19:41:08 +03:00
|
|
|
if(target.nodes[0].hasAttribute('download')) {
|
2024-11-30 15:35:43 +03:00
|
|
|
console.log('AJAX | Skipped because its download')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
if(!dom_url || dom_url == '#' || dom_url.indexOf('javascript:') != -1) {
|
2024-11-29 23:26:20 +03:00
|
|
|
console.log('AJAX | Skipped because its anchor or function call')
|
2024-11-28 17:43:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(target.attr('target') == '_blank') {
|
2024-11-29 23:26:20 +03:00
|
|
|
console.log('AJAX | Skipping because its _blank.')
|
2024-11-28 17:43:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!window.router.checkUrl(url)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
console.log(`AJAX | Going to URL ${url}`)
|
|
|
|
await window.router.route({
|
|
|
|
url: url,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
u(document).on('submit', 'form', async (e) => {
|
|
|
|
if(u('#ajloader').hasClass('shown')) {
|
|
|
|
e.preventDefault()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if((localStorage.getItem('ux.disable_ajax_routing') ?? 0) == 1 || window.openvk.current_id == 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-12-01 21:53:15 +03:00
|
|
|
if(window.openvk.disable_ajax == 1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
u('#ajloader').addClass('shown')
|
|
|
|
|
|
|
|
const form = e.target
|
|
|
|
const method = form.method ?? 'get'
|
|
|
|
const url = form.action
|
2024-12-01 22:12:40 +03:00
|
|
|
if(form.onsubmit || url.indexOf('/settings?act=interface') != -1) {
|
2024-11-28 23:15:50 +03:00
|
|
|
u('#ajloader').removeClass('shown')
|
|
|
|
return false
|
|
|
|
}
|
2024-12-01 22:12:40 +03:00
|
|
|
e.preventDefault()
|
2024-11-28 17:43:40 +03:00
|
|
|
|
|
|
|
const url_object = new URL(url)
|
|
|
|
if(method == 'get' || method == 'GET') {
|
|
|
|
url_object.searchParams.append('hash', window.router.csrf)
|
|
|
|
$(form).serializeArray().forEach(param => {
|
|
|
|
url_object.searchParams.append(param.name, param.value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!url) {
|
2024-11-28 23:15:50 +03:00
|
|
|
u('#ajloader').removeClass('shown')
|
2024-11-28 17:43:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-01 18:12:50 +03:00
|
|
|
const form_data = serializeForm(form, e.submitter)
|
2024-11-28 17:43:40 +03:00
|
|
|
const request_object = {
|
2024-11-29 23:26:20 +03:00
|
|
|
method: method,
|
|
|
|
headers: {
|
|
|
|
'X-OpenVK-Ajax-Query': '1',
|
|
|
|
}
|
2024-11-28 17:43:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(method != 'GET' && method != 'get') {
|
|
|
|
request_object.body = form_data
|
|
|
|
}
|
|
|
|
|
|
|
|
const form_res = await fetch(url_object, request_object)
|
|
|
|
const form_result = await form_res.text()
|
|
|
|
switch(form_res.status) {
|
|
|
|
case 500:
|
2024-11-28 23:15:50 +03:00
|
|
|
case 502:
|
2024-11-28 17:43:40 +03:00
|
|
|
makeError(form_res.statusText)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
const parser = new DOMParser
|
|
|
|
const parsed_content = parser.parseFromString(form_result, 'text/html')
|
|
|
|
|
|
|
|
if(form_res.redirected) {
|
|
|
|
history.replaceState({'from_router': 1}, '', form_res.url)
|
|
|
|
} else {
|
|
|
|
const __new_url = new URL(form_res.url)
|
|
|
|
__new_url.searchParams.delete('al')
|
|
|
|
__new_url.searchParams.delete('hash')
|
|
|
|
|
|
|
|
history.pushState({'from_router': 1}, '', __new_url)
|
|
|
|
}
|
2024-11-30 15:35:43 +03:00
|
|
|
|
2024-11-28 17:43:40 +03:00
|
|
|
window.router.__appendPage(parsed_content)
|
2024-11-29 23:26:20 +03:00
|
|
|
await window.router.__integratePage()
|
2024-11-28 17:43:40 +03:00
|
|
|
|
|
|
|
u('#ajloader').removeClass('shown')
|
|
|
|
})
|
2024-11-28 23:15:50 +03:00
|
|
|
|
|
|
|
window.addEventListener('popstate', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
window.router.route({
|
|
|
|
url: location.href,
|
|
|
|
push_state: false,
|
|
|
|
})
|
|
|
|
})
|