我可以区分给定的外部网站是否有“服务工作者”?
我最好的猜测是:
(我不确定如何做到这一点)
鉴于数据将是这样的:
const sitesToCheck = {
'www.site1.com',
'www.site2.com',
'www.site3.com',
}
答案 0 :(得分:8)
如果要在代码中执行此操作,则只能在与要检查的工作人员相同的原点内执行此操作。在同一来源中,您可以使用getRegistration(url)
(为您提供将解析为undefined
或ServiceWorkerRegistration
object为URL的承诺)或getRegistrations()
(这会给您对ServiceWorkerRegistration
objects数组的承诺。 E.g:
navigator.serviceWorker.getRegistrations().then(registrations => {
console.log(registrations);
});
答案 1 :(得分:4)
根据Nicolas的建议,可以使用无头Chrome浏览器进行此项检查。
以下是使用Puppeteer的示例:
public class TouchscreenDashboard extends AppCompatActivity implements View.OnClickListener {
int[] sounds={R.raw.decrease_fan_speed_by_2, R.raw.decrease_fan_speed_by_5,
R.raw.increase_fan_speed1, R.raw.increase_fan_speed_by_3,R.raw.lower_temp1,
R.raw.lower_temp4, R.raw.raise_temp2, R.raw.raise_temp3 ,R.raw.skip_backwards_2_songs,
R.raw.skip_backwards_4_songs,R.raw.skip_forward_1_song, R.raw.skip_forward_5_songs,
R.raw.volume_down3,R.raw.volume_down6, R.raw.volume_up3, R.raw.volume_up5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Log.i("DashboardActivity", "Dashboard has started");
startTimer();
}
public void startTimer() {
timer = new Timer();
timer2 = new Timer();
initializeTimerTask();
initializeTimerTask2();
timer.schedule(timerTask, randomTimeInt, randomTimeInt); //
timer2.schedule(timerTask2, randomTimeInt + 5000, randomTimeInt);
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
@Override
public void run() {
audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(audioAttributes)
.build();
releaseSoundpool = true;
Random r = new Random();
int Low = 0;
int High = 16;
int random = r.nextInt(High-Low) + Low;
final int soundId = soundPool.load(getApplicationContext(), sounds[random], 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(soundId, 1, 1, 1, 0, 1);
}
});
Log.d("TouchscreenDashboard", "soundID played: " + random);
}
};
}
public void initializeTimerTask2() {
timerTask2 = new TimerTask() {
@Override
public void run() {
soundPool.release();
Log.d("TouchscreenDashboard", "SoundPool released");
}
};
}
答案 2 :(得分:0)
您可以查看Service Worker Detector,这是一个Chrome扩展程序,可以通过阅读navigator.serviceWorker.controller
属性来检测网站是否注册了服务工作者。它可能也适用于other browsers supporting Web Extensions,但看起来它还没有这样分发。
但是,它需要在浏览器中运行脚本,这可能无法满足您的需求。您可以尝试使用可编写脚本的headless Chrome。