For these types of tasks, there are many solutions depending on the scenario and the way you want to approach them.
- Check whether the request path is different using
frappe.local.request.pathfor both requests?
or
- You can set flags (
frappe.flags.gl_preview = true) and check in your code iffrappe.flags.gl_previewis true, but for this, you will have to override thestock_controller.pycode or other ERPNext file code.