[英]How can I setup a cross-domain call in Tampermonkey to get a divs contents
我已經玩了幾天了,我覺得在tampermonkey腳本中成功收集字符串數據沒有什么距離。 我得到的最接近的結果是使用此代碼http://jsfiddle.net/peterbenoit/N7avm/成功加載了網頁/數據,但是一旦我嘗試將其添加為tampermonkey腳本,就會發生熟悉的跨域錯誤。
拒絕加載腳本“ http://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20html%20where…t = xml%27&callback = jQuery21407879608951028498_1460047110140&_ = 1460047110141”,因為它違反了以下內容內容安全政策指令:“ script-src'self''unsafe-inline''unsafe-eval'http: //steamcommunity-a.akamaihd.net/ https://api.steampowered.com/ http:// www。 google-analytics.com https://ssl.google-analytics.com https://www.google.com https://www.gstatic.com https://apis.google.com ”。
$(function() {
var container = $('#target');
var msg = $('#msg');
var url = 'http://peterbenoit.com';
$.getJSON("http://query.yahooapis.com/v1/public/yql?" +
"q=select%20*%20from%20html%20where%20url%3D%22" +
encodeURIComponent(url) +
"%22&format=xml'&callback=?",
function(data) {
if (data.results[0]) {
var data = filterData(data.results[0]);
msg.html('(ready.)');
container.html(data).focus().effect("highlight", {}, 1000);
} else {
msg.html('(error!)');
msg.addClass('error');
var errormsg = '<p>Error: could not load the page.</p>';
container.html(errormsg).focus().effect('highlight', { color: '#c00' }, 1000);
}
}
);
});
function filterData(data) {
// filter all the nasties out
// no body tags
data = data.replace(/<?\/body[^>]*>/g, '');
// no linebreaks
data = data.replace(/[\r|\n]+/g, '');
// no comments
data = data.replace(/<--[\S\s]*?-->/g, '');
// no noscript blocks
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
// no script blocks
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
// no self closing scripts
data = data.replace(/<script.*\/>/, '');
// [... add as needed ...]
return data;
}
因此,我想要做的事情是運行一個個人用戶腳本,該腳本在站點A上顯示內容,我希望它從站點B上的div收集一串數據(div class =“ preview k_wear_float”)並顯示在在var消息中放置一個站點。 我已經嘗試過iframe,JSON,xmlhttprequests,目前甚至在訪問數據時也沒有任何運氣,因此,如果有人可以幫助我指出正確的方向,那將是很好的。
我對js有基本的了解,但我仍然是一個新手,因此,請盡可能向普通用戶解釋,以使我能更好地理解該過程。
嘗試這個
$.ajax({
type:'GET',
url: "http://query.yahooapis.com/v1/public/yql?" + "q=select%20*%20from%20html%20where%20url%3D%22" + encodeURIComponent(url) + "%22&format=xml'&callback=?",
async:false,
contentType: "application/json",
dataType: 'jsonp'
}).done(function(data){
console.log(data)
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.