How to persistent background page in Safari extension - safari

I used xcrun safari-web-extension-converter to convert my chrome extension to safari extension but it built with this warning
Warning: Persistent background pages are not supported on iOS and iPadOS. You will need to make changes to support a non-persistent background page.
Also when using it on safari, it is showing these errors:
Extension errors in safari
How can I debug the errors in the extension? I'm not sure about the error but the persistent page warning seems to be a good place to start with. While searching for it in google, all I got is results for non-persistent background pages.
Do let me know if any more information is required.
PS:
The extension option in the "develop" menu of safari is also disabled due to service_worker failed to load error.

From WWDC21-10104:
The background page is a web page that the browser loads to run your
extension's background script. And this page allows your extension to
handle events sent by the browser or other parts of your extension.
But keeping this page loaded has a performance cost. It can use memory
and power as if you were keeping one more tab open and running for
every enabled extension. Keeping all these pages loaded all the time
can be pretty wasteful. But you can make a background page
non-persistent, which means the browser will only load it when your
extension actually needs to do work, and the browser can later unload
that page when it's been idle for some time. That way, the performance
cost is only paid while your extension is doing something useful. This
is important because background pages must be non-persistent on iOS,
where system memory and battery life are especially at a premium. The
web extension templates in Xcode already come with a non-persistent
background page, so they're ready to run on iOS. But if you have an
existing extension that uses a persistent background page like Sea
Creator did, you'll need to change it to be non-persistent. And you
can do that by adding that "persistent:" False key in the background
section of your manifest.
So you can solve it by adding this to your manifest.json:
"background": {
"scripts": [ "background.js" ]
"persistent": false
}

Related

How to handle user gesture required is required error from browsers

Hello I'm using neodynamic weblientprint to print directly from browser. There's a part that includes a script that detects if the client utility is installed, after which its retrives the printers installed on the system.
However I intermittently get this error when the script runs - Unable to launch 'webclientprintvi: -getPrinters: because a user gesture is required.
I get this error on some client system browsers, while some client systems browsers never show this error. Is there a setting that needs to be configured on the browser. Browsers tests are chrome and edge
Thanks.
I discovered that if you are using the WCPP detection feature, then calling jsWebClientPrint.getPrinters() will produce the "...because a user gesture is required" console error. I had a page that called that method w/o the detection and it worked w/o complaining.
I didn't dive any deeper than that as to determining why the extra embedding detection causes this to happen. I just re-worked the page, calling the method instead on a Refresh button click; which, of course Chrome is then happen with.

How do I disable cache in Safari 11.0?

I updated Safari today to 11.0 and the 'disable cache(s)' menu item (under Develop) is missing. Anyone know the new way to effect this?
As Adam has answered there was information about disable caches added to the release notes, and I've updated my answer accordingly:
For a one-off, you can use ⌥⌘R on the page to reload from origin or ⇧⌘R from Web Inspector.
For a more permanent solution, you can tell Safari to ignore cache when loading resources by opening Web Inspector then going to the Network tab and setting the icon in the top right (it turns blue when active).
After experimentation with the option in the Network tab, it doesn't appear to behave as documented in the release notes:
The setting appears to persist until manually disabled, even when quitting Safari.
Caching appears to be disabled for all pages, not just the inspected one.
Web Inspector didn't always need to be left open.
Resources served from localhost are sometimes still served from the memory cache, even if caching is disabled in the Network tab. Restarting Safari seemed to clear this behaviour.
Safari Technology Preview Release Notes (Release 27)
Browser Changes
Added a “Reload Page From Origin” alternate menu item to the View menu. This action reloads a page without using cached resources.
Removed the Option-Command-R (⌥⌘R) keyboard shortcut from “Enter/Exit Responsive Design Mode” and mapped it to “Reload Page From Origin” instead.
Removed the Disable Caches menu item in the Develop menu. The equivalent functionality is now available through Web Inspector’s Network tab.
Web Inspector
Added “Disable Caches” toggle in the Networking tab that only applies to the inspected page while Web Inspector is open.
Safari 11.1
The appearance of the icon has changed slightly in Safari 11.1:
It looks like (according to the “Safari Technology Preview Release Notes”) that the only way is to open the “Web Inspector” and go the the “Network” tab, and click the small button to ignore the resource cache.
From my testing on Safari 11.0.3, you should restart Safari after toggling "Ignore the resource cache when loading resources".

App has dynamic JS files, unable to debug in Safari Remote Web Inspector

I have a sapui5 app that I'm trying to test on mobile Safari using remote web inspector. Since it loads javascript files dynamically (uses MVC), it only lists these files under XHR. Apparently, because it's XHR, I can't add a breakpoint to these files.
Is there any way for me to debug this? Or force web inspector to load these files some place else? It seems a bit useless otherwise.
I have the latest version of iOS and Safari on my iPad.
I know this question has been asked previously but nobody answered it. So I'm hoping this one will gain a bit more traction.
Thanks!

Background js doesn't work on error pages

Is there any way to load background js even on error pages (such as net::ERR_NETWORK_CHANGED)? I need to keep persistence connection with WS server from the extension, but error pages don't load background js. So I lose the connection and possibility to restart it (due this is automated tool without access to browser ui).
The only solution I found is to use proxy server to customize error pages and load background js inside of them.
The assertion "Background js doesn't work on error pages" does not make any sense, because there's only one background page per extension two if you use split incognito mode.
So I assume that you want to detect a network connectivity loss in order to restore the web socket. Chrome offers two reliable global events for this: online and offline.
I have published the source code of Real-time desktop notifications for Stack Exchange inbox, which also accounts for network connectivity loss/regain. The relevant Web Socket part of the Chrome extension is found in stackexchange-notifications/Chrome/using-websocket.js on Github.

Safari 5.1 unloading tabs from memory

Well, it seems Safari 5.1 at least on Lion which is what I'm using now decides to unload tabs from memory as it deems it necessary.
The big issue with this is sites that display ajax alerts or ones that have chats as for example GMail. I was having 2 conversations on Google Talk right in the GMail website and while I was on another tab I noticed the title blinking indicating new messages in my conversations, as soon as I clicked the tab Safari decided to completely reload the page, losing all those conversations (I don't keep logs of those).
It goes without saying that this behavior breaks modern web apps and seems unnecessary on a desktop computer. I understand the necessity of this on iOS devices due to ram constrains though.
Is there any way to disable this "feature", I couldn't find a way to do so. It's that or switching to Chrome or Firefox, but I like Safari.
Thanks.
I think I have a temporary work around. Pages on my site — petersonguides.com — periodically reload the tab in Safari 5.1. Ordinarily this wouldn't be a big issue, but I have a video that plays in a FancyZoom box and it can't get all the way through without the tab being refreshed.
I tested with Chrome and it doesn't cause the same tab reload behavior, so I'm sure it's a Safari 5.1 problem.
I have JavaScript and PHP on the home page and I thought there might be a conflict, so I started pulling things off and retesting with the video. The last thing I checked was removing the Google Analytics script. That fixed the problem.
I tried bumping up the Database Storage amount - per the previous answer - but that didn't have any effect.
I am having trouble with Pandora (Flash) not advancing songs in a background tab. As a quick-and-dirty test, I went to Preferences > Advanced > Database storage and popped it up from 5MB to 500MB. Flash is still very unstable, but now songs are advancing in the background. I don't know if this will work for you but you might give it a try.
Safari 5.1 | OS X 10.6.8
The reloading of tabs in the background is a feature of Safari 5.1 on Lion. There is currently no easy way to get around this. The only way possible at the moment involves disabling the multi-process window feature:
Enable Safari's Debug menu by typing in Terminal.app, restarting Safari afterwards:
defaults write com.apple.Safari IncludeInternalDebugMenu 1
From the new Debug menu in Safari, select Use Multi-process Windows to remove the tick before it and deactivate this feature.
While this prevents tab reloading in the background it will disable extensions and gestures in Safari. But it can be easily activated again via the Debug menu.