jQuery + Greasemonkey:所有受影响的网站,不限于"window.location.href.indexOf"; [英] jQuery+Greasemonkey: All sites affected, not limited to "window.location.href.indexOf"
本文介绍了jQuery + Greasemonkey:所有受影响的网站,不限于"window.location.href.indexOf";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试让代码仅在特定的URL集上运行.对于我一生,我无法弄清楚为什么我单击的每个网站都会运行该代码,而不仅限于我所限制的网站.
I'm trying to have code ran only on specific set of URLs. For the life of me I can not figure out why every website I click, it runs the code, and not limited to the sites I limit it to.
// ==UserScript==
// @name test
// @namespace test1
// @description test2
// @include https://*
// @version 1
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js
// ==/UserScript==
$(document).ready(function ()
{ if( (!window.location.href.indexOf("https://www.youtube.com") > -1)
&& (!window.location.href.indexOf("https://www.google.com") > -1)
)
{
alert("test");
}
});
推荐答案
您已经关闭.只需尝试在数组中声明所有预期的url即可:
You were close. Just try declaring all the intended urls in an array :
JavaScript:
var urls = [
"https://www.youtube.com",
"https://www.google.com",
"https://fiddle.jshell.net",
];
urls.forEach(function(v) {
if (window.location.href.indexOf(v) >= 0) {
alert("test");
}
});
JSFiddle:
https://jsfiddle.net/nikdtu/hyj6gmgu/
这篇关于jQuery + Greasemonkey:所有受影响的网站,不限于"window.location.href.indexOf";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文