I can preview my code in app but can not work on desk, anyone can help to solve this issue?

I made a code to show the daily covid 19 situations before, it works well on the past but now it doesn’t work on desk while still work well in app. I don’t know if it is a bug. Anyone can help?

So your widget works when you run the script in the app, but not on your homescreen?

Some possible solutions:

  • look through your code if it does anything special when run in a widget. Look for config.runsInWidget and config.runsInApp.
  • how much free space do you have left? if it’s just a few GB, try to free some space.

I hope this helps!

Are you using stacks to position everything in the widget? It might be that you’ve exceeded the allowable RAM usage for the widget. I had this issue before with my Upcoming Calendar Indicator widget and had to make some optimizations to reduce the number of stacks used. However, I did have over 100 stacks included.

Thanks for your suggestion, here is the result I chcked :64GB space left and it return true with config. runsinapp.

Okay, so you’ve enough space left.

I wanted to know if you’ve used config.runsInApp or config.runsInWidget anywhere in your code.

I can’t help you any further since it is a very specific problem and depends on the script you wrote. I can only guess what the problem might be without the code. If you post the code, make sure that you remove any private information (e.g. API keys) before posting it.

1 Like

Are you on iOS 15? If yes, then the problem is probably in WebView.evaluateJavaScript.

Also someone else has this problem on this forum:

Wow that was fast, thanks for confirming! Yes, with iOS15, WebView.evaluateJavaScript is exactly where it keeps hanging.

Thanks a lot for your help. Here is the code and no private information included.
It is appreciate if you can test and check on your device.
IOS 14.6 with my device.

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: brain;
// Author: Pih
// Update: 2021.01.17

const newBG = false //设置新的背景

const innerColor = Color.purple()
const outerColor = Color.blue()
// =================================
const currentDate = new Date()
    
// let widget = new ListWidget()
 
const data = JSON.parse((await new Request("https://cdn.mdeer.com/data/yqstaticdata.js?callback=callbackstaticdata").loadString()).slice(19, -1))
const TodayIncr = JSON.parse((await new Request("https://i.snssdk.com/forum/ncov_data/?activeWidget=1&version_code=8.0.2&tma_jssdk_version=1.2.2.0&app_name=news_article&vid=64483C4C-DE4A-4124-BACF-5546776F0737&device_id=69751709094&channel=App%20Store&resolution=750*1334&aid=13&ab_version=660830%2C662176%2C1859937%2C662099%2C668774%2C2326904%2C2328670%2C2001177%2C1593455%2C2221909%2C2313808%2C2198370%2C2033166%2C668775%2C1419597%2C2247462%2C668779%2C1877263%2C2248067%2C2319078%2C2315780&ab_feature=794526%2C1662483&ab_group=794526%2C1662483&update_version_code=80215&openudid=17f543481771bc6001838ba84a0e131013835402&pos=5pe9vb%2F88Pzt3vTp5L%2B9p72%2FeBAKeScxv7GXvb2%2F%2FvTp5L%2B9p72%2FeBAKeScxv7GXvb2%2F8fLz%2BvTp6Pn4v72nvayspbOqqqWppa6lpauqrKWqqLGXvb2%2F8fzp9Ono%2Bfi%2Fvae9rqyzpKusq6irq6%2Bkqqqprq2qsZe9vb%2F88Pzt0fzp9Ono%2Bfi%2Fvae9rqyzpKusq6irq6%2Bkqqqprq2qsZe9vb%2F88Pzt0fLz%2BvTp6Pn4v72nvayspbOqqqWppa6lpauqrKWqqLGXvb2%2F8fL%2B%2FPHC8fzp%2BO7pwu3y7r%2B9p73ml729vb2%2F6fTw%2BO7p%2FPDtv72nvayrraSqrKWtq6mzqKuvqK%2Bxl729vb2%2F7e%2Fy6%2FTz%2Fvi%2Fvae9v3ssAnUWEr%2Bxl729vb2%2F%2FvTp5L%2B9p72%2FeBAKeScxv7GXvb29vb%2F%2B8vLv%2BfTz%2FOn4y%2Fzx6Pjuv72nveaXvb29vb29v%2FHy8%2Fr06ej5%2BL%2B9p72srKWzqqqlqaWupaWrqqylqqixl729vb29vb%2Fx%2FOn06ej5%2BL%2B9p72urLOkq6yrqKurr6SqqqmuraqXvb29veCxl729vb2%2F%2FPn57%2Fju7r%2B9p72%2FeywCdRYSegEceBAKeScxeCUfdAY1dRcseBIteBEnv5e9veCX4A%3D%3D&cdid=0CD24D50-9D99-4FD3-8D84-025B6B21CA2F&idfv=64483C4C-DE4A-4124-BACF-5546776F0737&ac=4G&os_version=14.2&status_bar_height=20&ssmix=a&device_platform=iphone&iid=1688502162686784&ab_client=a1%2Cf2%2Cf7%2Ce1&device_type=iPhone%207&idfa=00000000-0000-0000-0000-000000000000&data_type=%5B2%2C4%2C8%5D&src_type=map").loadJSON()).ncov_nation_data).confirmedIncrProvinceTop10

// log(data)

const covidnews = JSON.parse((await new Request("https://channel.chinanews.com/cns/s/5013.shtml?pager=0&_="+(Math.floor(currentDate.getTime()))).loadString()).slice(17,-29)).docs

const chartDrawing = new DrawContext()
chartDrawing.size = new Size(642, 221)
chartDrawing.opaque=false
chartDrawing.respectScreenScale=true

// ######

var max;
	for(i = 0; i<30 ;i++)
  {
	let	temp = data.incTrend[data.incTrend.length-30+i].sure_cnt;
		max = (temp > max || max == undefined ? temp : max)
	} 
const deltaY = 50/max
for(i = 0; i<30 ;i++)
{
let	temp = data.incTrend[data.incTrend.length+i-30].sure_cnt*deltaY 
let width = 8,color = Color.green()
if (i==29)
{
color = Color.red()}

fillRect(30+i*13, 100-temp, width, temp, 4, color)}

drawText(30, 25, 300, 60, "全国每日确诊 - 30日趋势图", Color.black(), "a",20, "left")

// ######
drawText(430, 20, 190, 60, "较昨日新增确诊", Color.black(), "a",20, "center")
drawText(430, 52, 190, 60, "+"+Math.round(data.incTrend[data.incTrend.length-1].sure_cnt).toString(), Color.red(), "a",47, "center")
fillRect(0, 111, 642, 110, 0, new Color("ffffff",0.3))

fillRect(0, 109, 642, 2, 0, Color.blue())

var IncrMax;
	for(i = 0; i<(TodayIncr.length) ;i++)
  {
if(TodayIncr[i].inboundIncr == undefined)
{TodayIncr[i].inboundIncr = 0}
let	temp = 
TodayIncr[i].confirmedIncr
// log("每项值"+temp)
	IncrMax = (temp > IncrMax || IncrMax == undefined ? temp : IncrMax)
	}
const incrDelta = 42/IncrMax
let gap = (425/TodayIncr.length)
// log ("长度:"+ TodayIncr.length+",gap:"+ gap)
for(i = 0; i<(TodayIncr.length) ;i++)
{
let	totalTemp = 
(TodayIncr[i].confirmedIncr)*incrDelta
fillRect(37+i*gap, 175-totalTemp, 21, totalTemp, 2, innerColor)
let	temp = 
(TodayIncr[i].inboundIncr)*incrDelta
fillRect(37+i*gap, 175-temp, 21, temp, 2, outerColor)
let inboundIncr = TodayIncr[i].inboundIncr
if(inboundIncr==0)
{inboundIncr=""}
drawText(25+i*gap, 177, 45, 20, inboundIncr.toString(), outerColor, "a",14, "center")//境外输入数
let increasedNum = TodayIncr[i].confirmedIncr- TodayIncr[i].inboundIncr
if(increasedNum== 0)
{increasedNum=""}
drawText(25+i*gap, 157-totalTemp, 45, 20, increasedNum.toString(), innerColor, "a",14, "center")//本土新增数
drawText(25+i*gap, 195, 45, 20, TodayIncr[i].name, Color.black(), "a",14, "center")//省份名称
}
fillRect(
460, 145, 40, 14, 0, innerColor)
drawText(
515,143, 190, 30, "本土新增", innerColor, "a",16, "left")
fillRect(
460, 170, 40, 14, 0, outerColor)
drawText(
515, 168, 190, 30, "境外输入", outerColor, "a",16, "left")
drawText(330, 120, 200, 60, "内地新增分布", Color.black(), "a",16, "left")

const widget = new ListWidget()
widget.setPadding(0, 0, 0, 0)

  
// ………………
// 背景选择………………
const files = FileManager.local()
const path = files.joinPath(files.documentsDirectory(), "convid19")
    if (newBG && config.runsInApp)
{
  const img = await Photos.fromLibrary()
  
      widget.backgroundImage = Addblur(img)
      files.writeImage(path, img)
}else{
//     if (config.runsInWidget) { 
//       try {
//     widget.backgroundImage = // Addblur(files.readImage(path))
//     log("读取图片成功")// 
// } catch (e){
//   widget.backgroundColor = new Color("000000", 1)
//   log(e.message)
// }    

//     } else {
      try {
    widget.backgroundImage = Addblur( files.readImage(path))
   log("读取图片成功")
} catch (e){
      const img = await Photos.fromLibrary()
      widget.backgroundImage = Addblur(img)
      files.writeImage(path, img)
      log(e.message)
    }
//   }
}
const scale = Device.screenScale()
const width = getWidgetSize().w
const height = getWidgetSize().h
// log (width)
// @@@@@@@@@@@@
// @@@@@@@@@@@@
if (config.runsInWidget==true)
{
// const contentStack = widget.addStack()
// contentStack.layoutVertically()
// contentStack.size = new Size(width/scale, height/scale)
// const InfoStack = contentStack.addStack()
// InfoStack.size = new Size(width/scale, (221/296)*height/scale)
// 
// const news1 = InfoStack.addText("【"+covidnews[0].pubtime.slice(5,16)+"】" + covidnews[0].title)
//   news1.font = Font.systemFont(10)
//   news1.url = covidnews[0].url
//   newsStack.addSpacer(5)
  widget.addText("hello world")
 
 // Finalize widget settings
//  widget.setPadding(16,16,16,0)
//  widget.spacing = -3
  
// InfoStack.addText("run in widget")
//(chartDrawing.getImage())
Script.setWidget(widget)
widget.presentMedium()
Script.complete()
}else{

const contentStack = widget.addStack()
contentStack.layoutVertically()
contentStack.size = new Size(width/scale, height/scale)
const InfoStack = contentStack.addStack()
InfoStack.size = new Size(width/scale, (221/296)*height/scale)
InfoStack.addImage(chartDrawing.getImage())
const newsStack = contentStack.addStack()
newsStack.backgroundColor = new Color("ffffff", 0.3)
newsStack.layoutVertically()
newsStack.size = new Size(width/scale, (75/296)*height/scale)
newsStack.setPadding(5, 10, 5, 15)
// ##################
const news1 = newsStack.addText("【"+covidnews[0].pubtime.slice(5,16)+"】" + covidnews[0].title)
  news1.font = Font.systemFont(10)
  news1.url = covidnews[0].url
  newsStack.addSpacer(5)
//   
//   
    const news2 = newsStack.addText("【"+covidnews[1].pubtime.slice(5,16)+"】" + covidnews[1].title)
  news2.font = Font.systemFont(10)
  news2.url = covidnews[1].url

  
// ##################

log("run in widget:"+config.runsInWidget)
log("run in app:"+config.runsInApp)
Script.setWidget(widget)
widget.presentMedium()

Script.complete()

}


function fillRect (x,y,width,height,cornerradio,color)
{
let path = new Path()
let rect = new Rect(x, y, width, height)
path.addRoundedRect(rect, cornerradio, cornerradio)
chartDrawing.addPath(path)
chartDrawing.setFillColor(color)
chartDrawing.fillPath()
}

function drawText(x, y, width,height,text,color,font,fontsize,alignment)
  {
    if (font=="a"){
chartDrawing.setFont(Font.boldRoundedSystemFont(fontsize))}
if (font=="default"){
chartDrawing.setFont(Font.lightMonospacedSystemFont(fontsize))}
if (font=="semibold"){
chartDrawing.setFont(Font.semiboldSystemFont(fontsize))}
  chartDrawing.setTextColor(color)
  if(alignment == "left")
  {chartDrawing.setTextAlignedLeft()}
  if(alignment == "center")
  {chartDrawing.setTextAlignedCenter()}
  if(alignment == "right")
  {chartDrawing.setTextAlignedRight()}
  chartDrawing.drawTextInRect(text, new Rect(x, y, width, height))
}

function Addblur (Img)
{
  const drawing = new DrawContext()
  drawing.size = Img.size
  const rect = new Rect(0, 0, drawing.size.width, drawing.size.height)
  drawing.drawImageInRect(Img, rect)
  drawing.setFillColor(new Color("000000", 0.))
  drawing.fillRect(rect)

  let blurImg = drawing.getImage()
return blurImg
}

function getWidgetSize ()
{
let deviceSize = (Device.screenSize().height*scale).toString()
let deviceInfo = 
  {  "2778": {
    "models"  : ["12 Pro Max"],
    "small"   : { "w": 510,   "h":  510 },
    "medium"  : { "w": 1092,  "h": 510 },
    "large"   : { "w": 1092,  "h": 1146}
  },

  "2532": {
    "models"  : ["12", "12 Pro"],
    "small"   : {"w": 474,  "h": 474 },
    "medium"  : {"w": 1014, "h": 474 },
    "large"   : {"w": 1014, "h": 1062 }
  },
   
  "2688": {
    "models"  : ["Xs Max", "11 Pro Max"],
    "small"   : {"w": 507,  "h": 507},
    "medium"  : {"w": 1080, "h": 507},
    "large"   : {"w": 1080, "h": 1137}
  },
  
  "1792": {
    "models"  : ["11", "Xr"],
    "small"   : {"w": 338, "h": 338},
    "medium"  : {"w": 720, "h": 338},
    "large"   : {"w": 720, "h": 758}
  },
  
  "2436": {
    "models"  : ["X", "Xs", "11 Pro"],
    "small"   : {"w": 465, "h": 465},
    "medium"  : {"w": 987, "h": 465},
    "large"   : {"w": 987, "h": 1035}
  },
  
  "2208": {
    "models"  : ["6+", "6s+", "7+", "8+"],
    "small"   : {"w": 471, "h": 471},
    "medium"  : {"w": 1044, "h": 471},
    "large"   : {"w": 1044, "h": 1071}
  },
  
  "1334": {
    "models"  : ["6","6s","7","8"],
    "small"   : {"w": 296, "h": 296},
    "medium"  : {"w": 642, "h": 296},
    "large"   : {"w": 642, "h": 648}
  },

  "1136": {
    "models"  : ["5","5s","5c","SE"],
    "small"   : {"w": 282, "h": 282},
    "medium"  : {"w": 584, "h": 282},
    "large"   : {"w": 584, "h": 622}
  }
}

let widgetSize = deviceInfo[deviceSize].medium
return widgetSize
// 
}

Please edit your post and format the code with three backticks like this:

```javascript
// your code goes here
```

Currently, the forum software edits the script in a way that it throws a syntax error. If you put it between the backticks tough, it doesn’t touch the script.

Updated according to your tips

Thank you for the edit!

I’ve tested it now on my iPhone with iOS 14.6 and the widget didn’t load. I’ve now checked the size of the responses and the one for TodayIncr returned 6.8 MB of data. That is quite a lot and it might push the memory use of the widget over the allowed limit. I don’t know what the limit is though.

I’ve then tried to run the script without this request like this and the widget works:

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: brain;
// Author: Pih
// Update: 2021.01.17

const newBG = false; //设置新的背景

const innerColor = Color.purple();
const outerColor = Color.blue();
// =================================
const currentDate = new Date();

// let widget = new ListWidget()

const data = JSON.parse(
  (
    await new Request(
      "https://cdn.mdeer.com/data/yqstaticdata.js?callback=callbackstaticdata"
    ).loadString()
  ).slice(19, -1)
);
const TodayIncr = [];
/*JSON.parse(
  (
    await new Request(
      "https://i.snssdk.com/forum/ncov_data/?activeWidget=1&version_code=8.0.2&tma_jssdk_version=1.2.2.0&app_name=news_article&vid=64483C4C-DE4A-4124-BACF-5546776F0737&device_id=69751709094&channel=App%20Store&resolution=750*1334&aid=13&ab_version=660830%2C662176%2C1859937%2C662099%2C668774%2C2326904%2C2328670%2C2001177%2C1593455%2C2221909%2C2313808%2C2198370%2C2033166%2C668775%2C1419597%2C2247462%2C668779%2C1877263%2C2248067%2C2319078%2C2315780&ab_feature=794526%2C1662483&ab_group=794526%2C1662483&update_version_code=80215&openudid=17f543481771bc6001838ba84a0e131013835402&pos=5pe9vb%2F88Pzt3vTp5L%2B9p72%2FeBAKeScxv7GXvb2%2F%2FvTp5L%2B9p72%2FeBAKeScxv7GXvb2%2F8fLz%2BvTp6Pn4v72nvayspbOqqqWppa6lpauqrKWqqLGXvb2%2F8fzp9Ono%2Bfi%2Fvae9rqyzpKusq6irq6%2Bkqqqprq2qsZe9vb%2F88Pzt0fzp9Ono%2Bfi%2Fvae9rqyzpKusq6irq6%2Bkqqqprq2qsZe9vb%2F88Pzt0fLz%2BvTp6Pn4v72nvayspbOqqqWppa6lpauqrKWqqLGXvb2%2F8fL%2B%2FPHC8fzp%2BO7pwu3y7r%2B9p73ml729vb2%2F6fTw%2BO7p%2FPDtv72nvayrraSqrKWtq6mzqKuvqK%2Bxl729vb2%2F7e%2Fy6%2FTz%2Fvi%2Fvae9v3ssAnUWEr%2Bxl729vb2%2F%2FvTp5L%2B9p72%2FeBAKeScxv7GXvb29vb%2F%2B8vLv%2BfTz%2FOn4y%2Fzx6Pjuv72nveaXvb29vb29v%2FHy8%2Fr06ej5%2BL%2B9p72srKWzqqqlqaWupaWrqqylqqixl729vb29vb%2Fx%2FOn06ej5%2BL%2B9p72urLOkq6yrqKurr6SqqqmuraqXvb29veCxl729vb2%2F%2FPn57%2Fju7r%2B9p72%2FeywCdRYSegEceBAKeScxeCUfdAY1dRcseBIteBEnv5e9veCX4A%3D%3D&cdid=0CD24D50-9D99-4FD3-8D84-025B6B21CA2F&idfv=64483C4C-DE4A-4124-BACF-5546776F0737&ac=4G&os_version=14.2&status_bar_height=20&ssmix=a&device_platform=iphone&iid=1688502162686784&ab_client=a1%2Cf2%2Cf7%2Ce1&device_type=iPhone%207&idfa=00000000-0000-0000-0000-000000000000&data_type=%5B2%2C4%2C8%5D&src_type=map"
    ).loadJSON()
  ).ncov_nation_data
).confirmedIncrProvinceTop10; */

// log(data)

const covidnews = JSON.parse(
  (
    await new Request(
      "https://channel.chinanews.com/cns/s/5013.shtml?pager=0&_=" +
        Math.floor(currentDate.getTime())
    ).loadString()
  ).slice(17, -29)
).docs;

const chartDrawing = new DrawContext();
chartDrawing.size = new Size(642, 221);
chartDrawing.opaque = false;
chartDrawing.respectScreenScale = true;

// ######

var max;
for (i = 0; i < 30; i++) {
  let temp = data.incTrend[data.incTrend.length - 30 + i].sure_cnt;
  max = temp > max || max == undefined ? temp : max;
}
const deltaY = 50 / max;
for (i = 0; i < 30; i++) {
  let temp = data.incTrend[data.incTrend.length + i - 30].sure_cnt * deltaY;
  let width = 8,
    color = Color.green();
  if (i == 29) {
    color = Color.red();
  }

  fillRect(30 + i * 13, 100 - temp, width, temp, 4, color);
}

drawText(
  30,
  25,
  300,
  60,
  "全国每日确诊 - 30日趋势图",
  Color.black(),
  "a",
  20,
  "left"
);

// ######
drawText(430, 20, 190, 60, "较昨日新增确诊", Color.black(), "a", 20, "center");
drawText(
  430,
  52,
  190,
  60,
  "+" + Math.round(data.incTrend[data.incTrend.length - 1].sure_cnt).toString(),
  Color.red(),
  "a",
  47,
  "center"
);
fillRect(0, 111, 642, 110, 0, new Color("ffffff", 0.3));

fillRect(0, 109, 642, 2, 0, Color.blue());

var IncrMax;
for (i = 0; i < TodayIncr.length; i++) {
  if (TodayIncr[i].inboundIncr == undefined) {
    TodayIncr[i].inboundIncr = 0;
  }
  let temp = TodayIncr[i].confirmedIncr;
  // log("每项值"+temp)
  IncrMax = temp > IncrMax || IncrMax == undefined ? temp : IncrMax;
}
const incrDelta = 42 / IncrMax;
let gap = 425 / TodayIncr.length;
// log ("长度:"+ TodayIncr.length+",gap:"+ gap)
for (i = 0; i < TodayIncr.length; i++) {
  let totalTemp = TodayIncr[i].confirmedIncr * incrDelta;
  fillRect(37 + i * gap, 175 - totalTemp, 21, totalTemp, 2, innerColor);
  let temp = TodayIncr[i].inboundIncr * incrDelta;
  fillRect(37 + i * gap, 175 - temp, 21, temp, 2, outerColor);
  let inboundIncr = TodayIncr[i].inboundIncr;
  if (inboundIncr == 0) {
    inboundIncr = "";
  }
  drawText(
    25 + i * gap,
    177,
    45,
    20,
    inboundIncr.toString(),
    outerColor,
    "a",
    14,
    "center"
  ); //境外输入数
  let increasedNum = TodayIncr[i].confirmedIncr - TodayIncr[i].inboundIncr;
  if (increasedNum == 0) {
    increasedNum = "";
  }
  drawText(
    25 + i * gap,
    157 - totalTemp,
    45,
    20,
    increasedNum.toString(),
    innerColor,
    "a",
    14,
    "center"
  ); //本土新增数
  drawText(
    25 + i * gap,
    195,
    45,
    20,
    TodayIncr[i].name,
    Color.black(),
    "a",
    14,
    "center"
  ); //省份名称
}
fillRect(460, 145, 40, 14, 0, innerColor);
drawText(515, 143, 190, 30, "本土新增", innerColor, "a", 16, "left");
fillRect(460, 170, 40, 14, 0, outerColor);
drawText(515, 168, 190, 30, "境外输入", outerColor, "a", 16, "left");
drawText(330, 120, 200, 60, "内地新增分布", Color.black(), "a", 16, "left");

const widget = new ListWidget();
widget.setPadding(0, 0, 0, 0);

// ………………
// 背景选择………………
const files = FileManager.local();
const path = files.joinPath(files.documentsDirectory(), "convid19");
if (newBG && config.runsInApp) {
  const img = await Photos.fromLibrary();

  widget.backgroundImage = Addblur(img);
  files.writeImage(path, img);
} else {
  //     if (config.runsInWidget) {
  //       try {
  //     widget.backgroundImage = // Addblur(files.readImage(path))
  //     log("读取图片成功")//
  // } catch (e){
  //   widget.backgroundColor = new Color("000000", 1)
  //   log(e.message)
  // }

  //     } else {
  try {
    widget.backgroundImage = Addblur(files.readImage(path));
    log("读取图片成功");
  } catch (e) {
//     const img = await Photos.fromLibrary();
//     widget.backgroundImage = Addblur(img);
//     files.writeImage(path, img);
//     log(e.message);
  }
  //   }
}
const scale = Device.screenScale();
const width = getWidgetSize().w;
const height = getWidgetSize().h;
// log (width)
// @@@@@@@@@@@@
// @@@@@@@@@@@@
if (config.runsInWidget == true) {
  // const contentStack = widget.addStack()
  // contentStack.layoutVertically()
  // contentStack.size = new Size(width/scale, height/scale)
  // const InfoStack = contentStack.addStack()
  // InfoStack.size = new Size(width/scale, (221/296)*height/scale)
  //
  // const news1 = InfoStack.addText("【"+covidnews[0].pubtime.slice(5,16)+"】" + covidnews[0].title)
  //   news1.font = Font.systemFont(10)
  //   news1.url = covidnews[0].url
  //   newsStack.addSpacer(5)
  widget.addText("hello world");

  // Finalize widget settings
  //  widget.setPadding(16,16,16,0)
  //  widget.spacing = -3

  // InfoStack.addText("run in widget")
  //(chartDrawing.getImage())
  Script.setWidget(widget);
//   widget.presentMedium();
  Script.complete();
} else {
  const contentStack = widget.addStack();
  contentStack.layoutVertically();
  contentStack.size = new Size(width / scale, height / scale);
  const InfoStack = contentStack.addStack();
  InfoStack.size = new Size(width / scale, ((221 / 296) * height) / scale);
  InfoStack.addImage(chartDrawing.getImage());
  const newsStack = contentStack.addStack();
  newsStack.backgroundColor = new Color("ffffff", 0.3);
  newsStack.layoutVertically();
  newsStack.size = new Size(width / scale, ((75 / 296) * height) / scale);
  newsStack.setPadding(5, 10, 5, 15);
  // ##################
  const news1 = newsStack.addText(
    "【" + covidnews[0].pubtime.slice(5, 16) + "】" + covidnews[0].title
  );
  news1.font = Font.systemFont(10);
  news1.url = covidnews[0].url;
  newsStack.addSpacer(5);
  //
  //
  const news2 = newsStack.addText(
    "【" + covidnews[1].pubtime.slice(5, 16) + "】" + covidnews[1].title
  );
  news2.font = Font.systemFont(10);
  news2.url = covidnews[1].url;

  // ##################

  log("run in widget:" + config.runsInWidget);
  log("run in app:" + config.runsInApp);
  Script.setWidget(widget);
  widget.presentMedium();

  Script.complete();
}

function fillRect(x, y, width, height, cornerradio, color) {
  let path = new Path();
  let rect = new Rect(x, y, width, height);
  path.addRoundedRect(rect, cornerradio, cornerradio);
  chartDrawing.addPath(path);
  chartDrawing.setFillColor(color);
  chartDrawing.fillPath();
}

function drawText(x, y, width, height, text, color, font, fontsize, alignment) {
  if (font == "a") {
    chartDrawing.setFont(Font.boldRoundedSystemFont(fontsize));
  }
  if (font == "default") {
    chartDrawing.setFont(Font.lightMonospacedSystemFont(fontsize));
  }
  if (font == "semibold") {
    chartDrawing.setFont(Font.semiboldSystemFont(fontsize));
  }
  chartDrawing.setTextColor(color);
  if (alignment == "left") {
    chartDrawing.setTextAlignedLeft();
  }
  if (alignment == "center") {
    chartDrawing.setTextAlignedCenter();
  }
  if (alignment == "right") {
    chartDrawing.setTextAlignedRight();
  }
  chartDrawing.drawTextInRect(text, new Rect(x, y, width, height));
}

function Addblur(Img) {
  const drawing = new DrawContext();
  drawing.size = Img.size;
  const rect = new Rect(0, 0, drawing.size.width, drawing.size.height);
  drawing.drawImageInRect(Img, rect);
  drawing.setFillColor(new Color("000000", 0));
  drawing.fillRect(rect);

  let blurImg = drawing.getImage();
  return blurImg;
}

function getWidgetSize() {
  let deviceSize = (Device.screenSize().height * scale).toString();
  let deviceInfo = {
    2778: {
      models: ["12 Pro Max"],
      small: { w: 510, h: 510 },
      medium: { w: 1092, h: 510 },
      large: { w: 1092, h: 1146 },
    },

    2532: {
      models: ["12", "12 Pro"],
      small: { w: 474, h: 474 },
      medium: { w: 1014, h: 474 },
      large: { w: 1014, h: 1062 },
    },

    2688: {
      models: ["Xs Max", "11 Pro Max"],
      small: { w: 507, h: 507 },
      medium: { w: 1080, h: 507 },
      large: { w: 1080, h: 1137 },
    },

    1792: {
      models: ["11", "Xr"],
      small: { w: 338, h: 338 },
      medium: { w: 720, h: 338 },
      large: { w: 720, h: 758 },
    },

    2436: {
      models: ["X", "Xs", "11 Pro"],
      small: { w: 465, h: 465 },
      medium: { w: 987, h: 465 },
      large: { w: 987, h: 1035 },
    },

    2208: {
      models: ["6+", "6s+", "7+", "8+"],
      small: { w: 471, h: 471 },
      medium: { w: 1044, h: 471 },
      large: { w: 1044, h: 1071 },
    },

    1334: {
      models: ["6", "6s", "7", "8"],
      small: { w: 296, h: 296 },
      medium: { w: 642, h: 296 },
      large: { w: 642, h: 648 },
    },

    1136: {
      models: ["5", "5s", "5c", "SE"],
      small: { w: 282, h: 282 },
      medium: { w: 584, h: 282 },
      large: { w: 584, h: 622 },
    },
  };

  let widgetSize = deviceInfo[deviceSize].medium;
  return widgetSize;
  //
}

The script like this runs fine in the widget and prints hello world.
Inserting the line TodayIncr.push({confirmedIncr: 0}) after line 24 to add one element to TodayIncr crashes the app for me though when running it (widget not tested).

To summarize, I strongly suspect that the response for the widget is too large with 6.8 MB. There might be the possibility that the response holds some data that crashes the script (like in my try with the added object) is quite small, since it doesn’t crash when run in the app with the actual response.

You can try to use the WebView to load the request, since this runs in a separate process as I’ve read somewhere. This might enable you to work around the memory issue, but I can’t say for sure that it will work.

1 Like

In addition, if you don’t need all of the JSON, you could try only grabbing portions of it. I had some issues before with memory in widgets and had to find ways to reduce the amount of memory being using while it was loading