Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Add In: Clicking a link to a different domain works, but changing the location with JS doesn't

I want to redirect my add in to a URL in a different domain.

I added the domain to the manifest like this:

<AppDomains>
   <AppDomain>https://another.domain.com</AppDomain>
</AppDomains>

And I can verify that worked because I can add a normal link to my add in like this:

<a href="https://another.domain.com/something">Click me</a>

Then I click that link and everything is fine.

But if I try to change the location programmatically like this:

window.location.href = "https://another.domain.com/something";

then the add in stops working, showing only the error screen saying "ADD-IN ERROR Sorry, we can't load the add-in. Please make sure you have network and/or internet connectivity. Click "Retry" once you're back online."

Is that the expected behaviour? How can I redirect my add in to a different domain? This used to work fine with the IE11 webview, but it doesn't work with the Chromium webview.

like image 608
Victor Avatar asked Nov 07 '25 02:11

Victor


1 Answers

I'm not an expert in Office add-ins, but as far as I understand, when you use WebView2 in an Office add-in the WebView2 window runs in an IFRAME, while the main document is an internal Office window.

Now when you inject scripts via CoreWebView2.ExecuteScriptAsync(String)method, these scripts are injected to the main document according to the docs: CoreWebView2.ExecuteScriptAsync(String):

Runs JavaScript code from the javaScript parameter in the current top-level document rendered in the WebView.

This means that window will point to the internal Office main window, not your WebView2. When you try to navigate this internal window, it crashes. When you instead have a link in your WebView2 it navigates the WebView2 iframe.

I see 2 solutions:

1. Get access to the Iframe window object:

window.frames[0].location.href = "https://another.domain.com/something";

Here I assume that there's only one iframe. Otherwise iterate through window.frames.

2. Load your javascript directly into your document:

Add a script tag to your <head> element with:

<script src="https://example.com/script.js"><script>.

Now window will be the WebView2window. This requires that you control the html. If you do, I recommend this method.

like image 132
Poul Bak Avatar answered Nov 08 '25 15:11

Poul Bak