web: Qualify global uses of location and history.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-06-07 15:39:47 -07:00 committed by Tim Abbott
parent 97fd5b8c6f
commit fa91a4493c
12 changed files with 31 additions and 27 deletions

View File

@ -133,7 +133,7 @@
<script nonce="{{ csp_nonce }}">
document.addEventListener('DOMContentLoaded', function () {
function reload() {
location.reload(true);
window.location.reload(true);
}
document.querySelectorAll('.reload-lnk').forEach(lnk => lnk.addEventListener('click', reload));
});

View File

@ -87,10 +87,14 @@ export function create_ajax_request(
$(form_error).hide();
$(form_success).show();
if (["autopay", "invoice"].includes(form_name)) {
if ("pushState" in history) {
history.pushState("", document.title, location.pathname + location.search);
if ("pushState" in window.history) {
window.history.pushState(
"",
document.title,
window.location.pathname + window.location.search,
);
} else {
location.hash = "";
window.location.hash = "";
}
}
success_callback(response);

View File

@ -111,8 +111,8 @@ $(() => {
// form otherwise the sponsorship pending message is partially
// hidden due to browser preserving scroll position.
// https://developer.mozilla.org/en-US/docs/Web/API/History/scrollRestoration
if (history.scrollRestoration) {
history.scrollRestoration = "manual";
if (window.history.scrollRestoration) {
window.history.scrollRestoration = "manual";
}
initialize();

View File

@ -1,4 +1,4 @@
// TODO: Rewrite this module to use history.pushState.
// TODO: Rewrite this module to use window.history.pushState.
import * as blueslip from "./blueslip";
import * as hash_parser from "./hash_parser";
@ -129,10 +129,10 @@ export function set_hash(hash: string): void {
// Avoid adding duplicate entries in browser history.
return;
}
if (history.pushState) {
if (window.history.pushState) {
const url = get_full_url(hash);
try {
history.pushState(null, "", url);
window.history.pushState(null, "", url);
update_web_public_hash(hash);
} catch (error) {
if (error instanceof TypeError) {
@ -163,12 +163,12 @@ type StateData = {
export function update_current_history_state_data(new_data: StateData): void {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const current_state = history.state as StateData | null;
const current_state = window.history.state as StateData | null;
const current_state_data = {
narrow_pointer: current_state?.narrow_pointer,
narrow_offset: current_state?.narrow_offset,
show_more_topics: current_state?.show_more_topics,
};
const state_data = {...current_state_data, ...new_data};
history.replaceState(state_data, "", window.location.href);
window.history.replaceState(state_data, "", window.location.href);
}

View File

@ -60,8 +60,8 @@ function show_all_message_view() {
message_view.show([{operator: "in", operand: "home"}], {
trigger: "hashchange",
change_hash: false,
then_select_id: history.state?.narrow_pointer,
then_select_offset: history.state?.narrow_offset,
then_select_id: window.history.state?.narrow_pointer,
then_select_offset: window.history.state?.narrow_offset,
});
}
@ -192,7 +192,7 @@ function do_hashchange_normal(from_reload) {
}
}
const data_for_hash = history.state;
const data_for_hash = window.history.state;
if (data_for_hash) {
narrow_opts.then_select_id = data_for_hash.narrow_pointer;
narrow_opts.then_select_offset = data_for_hash.narrow_offset;
@ -281,7 +281,7 @@ function do_hashchange_overlay(old_hash) {
if (base === "organization") {
settings_panel_object = settings_panel_menu.org_settings;
}
history.replaceState(
window.history.replaceState(
null,
"",
browser_history.get_full_url(base + "/" + settings_panel_object.current_tab()),
@ -300,7 +300,7 @@ function do_hashchange_overlay(old_hash) {
// not match the window.location.hash, then we also reset the
// base string we're tracking for the hash.
if (valid_hash !== window.location.hash) {
history.replaceState(null, "", browser_history.get_full_url(valid_hash));
window.history.replaceState(null, "", browser_history.get_full_url(valid_hash));
section = hash_parser.get_current_hash_section();
base = hash_parser.get_current_hash_category();
}
@ -309,7 +309,7 @@ function do_hashchange_overlay(old_hash) {
if (base === "groups") {
const valid_hash = hash_util.validate_group_settings_hash(window.location.hash);
if (valid_hash !== window.location.hash) {
history.replaceState(null, "", browser_history.get_full_url(valid_hash));
window.history.replaceState(null, "", browser_history.get_full_url(valid_hash));
section = hash_parser.get_current_hash_section();
}
}

View File

@ -174,7 +174,7 @@ function create_and_update_message_list(filter, id_info, opts) {
// workflow we have which calls `message_view.show` after hash is updated.
if (opts.change_hash) {
update_hash_to_match_filter(filter, opts.trigger);
opts.show_more_topics = history.state?.show_more_topics ?? false;
opts.show_more_topics = window.history.state?.show_more_topics ?? false;
}
// Show the new set of messages. It is important to set message_lists.current to

View File

@ -762,7 +762,7 @@ export function exclude_me_from_string(user_ids_string: string): string {
}
export function format_small_avatar_url(raw_url: string): string {
const url = new URL(raw_url, location.origin);
const url = new URL(raw_url, window.location.origin);
url.search += (url.search ? "&" : "") + "s=50";
return url.href;
}
@ -843,7 +843,7 @@ export function small_avatar_url_for_person(person: User): string {
function medium_gravatar_url_for_email(email: string): string {
const hash = md5(email.toLowerCase());
const avatar_url = "https://secure.gravatar.com/avatar/" + hash + "?d=identicon";
const url = new URL(avatar_url, location.origin);
const url = new URL(avatar_url, window.location.origin);
url.search += (url.search ? "&" : "") + "s=500";
return url.href;
}

View File

@ -4,7 +4,7 @@ $(() => {
// This code will be executed when the user visits /login and
// dev_login.html is rendered.
if ($("[data-page-id='dev-login']").length > 0 && window.location.hash.startsWith("#")) {
/* We append the location.hash to the input field with name next so that URL can be
/* We append the window.location.hash to the input field with name next so that URL can be
preserved after user is logged in. See this:
https://stackoverflow.com/questions/5283395/url-hash-is-persisting-between-redirects */
$("input[name='next']").each(function () {

View File

@ -207,7 +207,7 @@ $(document).on("click", ".pricing-tab", function () {
$(".subheader-filler").attr("colspan", plans_columns_count);
}
history.pushState(null, null, `#${id}`);
window.history.pushState(null, null, `#${id}`);
});
$(document).on("click", ".comparison-tab", function () {

View File

@ -10,11 +10,11 @@ import * as message_view from "./message_view";
// done before the first call to get_events
export function initialize() {
// location.hash should be e.g. `#reload:12345123412312`
if (!location.hash.startsWith("#reload:")) {
// window.location.hash should be e.g. `#reload:12345123412312`
if (!window.location.hash.startsWith("#reload:")) {
return;
}
const hash_fragment = location.hash.slice("#".length);
const hash_fragment = window.location.hash.slice("#".length);
const trigger = "reload";
// Using the token, recover the saved pre-reload data from local

View File

@ -90,7 +90,7 @@ if (page_params.server_sentry_dsn) {
return {
...context,
metadata: {source: "custom"},
name: normalize_path(location.pathname, sentry_key === "www"),
name: normalize_path(window.location.pathname, sentry_key === "www"),
};
},
shouldCreateSpanForRequest,

View File

@ -62,6 +62,6 @@ export function toggle_org_setting_collapse() {
// If current tab is about to be collapsed, go to default tab.
const $current_tab = $(".org-settings-list .active");
if ($current_tab.hasClass("hide-org-settings")) {
$(location).attr("href", "/#organization/organization-profile");
window.location.href = "/#organization/organization-profile";
}
}