jQuery+Greasemonkey:所有受影响的站点,不限于 "window.location.href.indexOf"
jQuery+Greasemonkey: All sites affected, not limited to "window.location.href.indexOf"
我试图让代码 运行 只在特定的一组 URL 上。对于我的生活,我无法弄清楚为什么我点击的每个网站都会运行代码,而且不限于我限制的网站。
// ==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");
}
});
var domains = [
'http://stacksnippets.net', // ***
'http://whosebug.com',
'http://google.com',
'http://youtube.com'
];
var href = window.location.href;
var isListed = domains.some(function(v){
// expecting href.indexOf(v) to return 0 on success
return !href.indexOf(v);
});
console.log(window.location.href, isListed)
window.location.href.indexOf("https://www.youtube.com") // is 0 OR -1
所以
!window.location.href.indexOf("https://www.youtube.com") // is true OR false
和
true > -1 // is always true
false > -1 // is always true
像这样的东西应该会让你到达那里。
var domains = [
'http://whosebug.com',
'http://google.com',
'http://youtube.com'
];
var href = window.location.href;
var isListed = domains.some(function(v){
return !href.indexOf(v);
});
isListed // true
你很接近。只需尝试在数组中声明所有预期的 url:
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:
我试图让代码 运行 只在特定的一组 URL 上。对于我的生活,我无法弄清楚为什么我点击的每个网站都会运行代码,而且不限于我限制的网站。
// ==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");
}
});
var domains = [
'http://stacksnippets.net', // ***
'http://whosebug.com',
'http://google.com',
'http://youtube.com'
];
var href = window.location.href;
var isListed = domains.some(function(v){
// expecting href.indexOf(v) to return 0 on success
return !href.indexOf(v);
});
console.log(window.location.href, isListed)
window.location.href.indexOf("https://www.youtube.com") // is 0 OR -1
所以
!window.location.href.indexOf("https://www.youtube.com") // is true OR false
和
true > -1 // is always true
false > -1 // is always true
像这样的东西应该会让你到达那里。
var domains = [
'http://whosebug.com',
'http://google.com',
'http://youtube.com'
];
var href = window.location.href;
var isListed = domains.some(function(v){
return !href.indexOf(v);
});
isListed // true
你很接近。只需尝试在数组中声明所有预期的 url:
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: