Forum Discussion
Is there a way around running a custom script before all scripts on any web page
Hi,
Is there a way around running a custom script before all scripts on any web page in SharePoint Online Development?
I know how to create extension component and web parts with https://learn.microsoft.com/en-us/sharepoint/dev/spfx/sharepoint-framework-overview, but I found its scripts is loaded by the framework and is executed too late.
So I want to find a way to load my script for any page, including classic and modern pages, in order to take over the data (what data are allowed to be fetched). Besides, my script doens't depend on SharePoint Online objects, and replace the service worker (https://stackoverflow.com/questions/36000127/can-i-have-multiple-service-workers-both-intercept-the-same-fetch-request).
After referring tohttps://learn.microsoft.com/en-us/sharepoint/dev/spfx/sharepoint-framework-overview#key-features-of-the-sharepoint-framework
Important
The SharePoint page https://www.w3.org/TR/WD-DOM/introduction.html is not an API. You should avoid taking any dependencies on the page DOM structure or CSS styles, which are subject to change and potentially break your solutions. SharePoint Framework provides a rich API to customize the SharePoint experience in reliable ways and is the only supported means to interact with the SharePoint page HTML DOM.
I found that it is best to do the client-side control by the public REST interfaces used by https://pnp.github.io/pnpjs/ (a safe API for interacting with https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-to-know-the-sharepoint-rest-service?tabs=csom#how-the-sharepoint-rest-service-works and https://learn.microsoft.com/en-us/graph/use-the-api#call-a-rest-api-method) because I think SPFx is mainly applicable for UI function (beautification) after data reach the client browser.
Thanks!