简单用户脚本运行缓慢,有时无法正常工作

请注意,我是JS新手,这是我的第一个脚本。

我正在编写一个小脚本,该脚本应该允许我使用键盘快捷键在未聚焦的newtab中打开flickr页面中的最大可用图像。

我目前面临两个问题:

  1. 该脚本加载缓慢,这意味着如果我使用箭头键更改幻灯片中的图像,则需要等待1或2秒钟,然后才能按键盘快捷键在newtab中打开图像。如果我不等,它将根据我跳过这些图像的速度在新选项卡中打开幻灯片显示的先前图像之一。 (它应该始终在新标签页中打开我当前正在查看的图像)

  2. 我使用log(mainurl)打印“ mainurl”的当前内容,该内容是指向当前打开的图像的最大可用大小的链接。但是由于某些原因,它总是为我提供上一张图片的网址,具体取决于我跳过幻灯片显示的速度(即使在新标签页中打开了正确的图片)。

如果要检查脚本,这里是flickr帐户的URL。 (必须在照片流[幻灯片放映模式]下运行)URL to flickr photostream

这是我写的代码

from pyspark.sql.functions import count
from pyspark.sql.functions import pandas_udf,PandasUDFType
from scipy.stats import entropy



# Define a UDAF
@pandas_udf("double",PandasUDFType.GROUPED_AGG)
def my_entropy(data):

    p_data = data.value_counts()           # counts occurrence of each value
    s = entropy(p_data)  # get entropy from counts
    return s


# Perform a groupby-agg 
groupby_col = "a_column"
agg_col = "another_column"
df2return = df\
    .groupBy(groupby_cols)\
    .agg(count(agg_col).alias("count"),my_entropy(agg_col).alias("s"))

df2return.show()

非常感谢您的帮助!

syl1981 回答:简单用户脚本运行缓慢,有时无法正常工作

最好拦截站点对其服务器API的现有快速请求(您可以在devtools网络面板中对其进行检查),而不是发出其他缓慢请求。

为此,我们将使用unsafeWindow并挂钩XMLHttpRequest原型。另外,请在document-start处运行脚本,以便在页面发出请求之前,它会附加拦截器。

// ==UserScript==
// @name        Flickr Max NewTab
// @match       https://www.flickr.com/*
// @grant       GM_openInTab
// @run-at      document-start
// ==/UserScript==

const xhrOpen = unsafeWindow.XMLHttpRequest.prototype.open;
unsafeWindow.XMLHttpRequest.prototype.open = function (method,url) {
  if (/method=[^&]*?(getPhotos|getInfo)/.test(url)) {
    this.addEventListener('load',onXhrLoad,{once: true});
  }
  return xhrOpen.apply(this,arguments);
};

const imgUrls = {};

function onXhrLoad() {
  const json = JSON.parse(this.response);
  const photos = json.photos ? json.photos.photo : [json.photo];
  for (const {id,url_o} of photos) {
    imgUrls[id] = url_o;
  }
}

document.addEventListener('keydown',e => {
  if (e.altKey && e.code === 'KeyQ' && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
    const id = location.pathname.split('/')[3];
    const url = imgUrls[id];
    if (url) GM_openInTab(url,true);
  }
});
本文链接:https://www.f2er.com/3159304.html

大家都在问