gpt4 book ai didi

javascript - Chrome 扩展程序访问 Xively

转载 作者:行者123 更新时间:2023-11-28 01:14:29 25 4
gpt4 key购买 nike

如何通过 Chrome 扩展程序访问 Xively Feed。我正在构建一个简单的扩展,它将根据我的 Xively Feed 上当前显示的值来阻止网站。不过,我不确定如何在没有 Html 的情况下访问 Xively feed,我查看了之前在 HTML 页面中使用过的 XivelyJS 库。但是我的扩展需要独立于 HTML。

下面的代码是我使用 XivelyJS 的尝试,但我会很感激更简单或更好的方法。

背景.js:

     chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {cancel: true};


},
{urls: ["*://www.facebook.com/*", "*://www.twitter.com/*"]},
["blocking"]);



// Set your API key first

// Get feed content
xively.setKey( "APIKEY" );

function ReadData(){

var reading = xively.datastream.get ("832936337", "Reading", callback (data));
console.log(reading);
console.log(data);
setTimeout(ReadData,3000);
}
ReadData();

Manifest.json:

{
"manifest_version": 2,
"name": "SiteBlockerFyp",
"description": "Block Respective Sites",
"version": "1",

"permissions": [
"webRequest",
"webRequestBlocking",
"*://www.facebook.com/*","*://www.twitter.com/*"

],
"content_scripts": [
{
"matches": ["*://www.facebook.com/*"],

"js": ["jquery.min.js", "xivelyjs.min.js"]
}
],

"background": {"scripts": ["background.js"]}


}

最佳答案

发现我可以像这样访问值:

     chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {cancel: true};
console.log(details.url);
},
{urls: ["*://www.twitter.com/*"]},
["blocking"]);








// Set your API key first

// Get feed content
xively.setKey( "APIKEY" );
console.log("XIVELY LOADED");

var feedID = 832936337, // Feed ID
datastreamID = "Reading"; // Datastream ID
data = "null"; // Your element on the page
dataUpdated= "null";


console.log("READ ");


xively.datastream.get (feedID, datastreamID, function ( datastream ) {

data = datastream["current_value"];
console.log(data);

xively.datastream.subscribe( feedID, datastreamID, function ( event , datastream_updated ) {

dataUpdated = datastream_updated["current_value"] ;
console.log(dataUpdated);
});

});

chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
console.log(tabs[0].url);
if (tabs[0].url == "*www.twitter.com*") {
alert("Twitter");
}

}
);

订阅还为我提供了最新消息。然而,在 list 中使用background.js作为后台脚本意味着它不会在所有页面上运行,所以我仍在尝试解决这个问题,内容脚本显然是可行的方法。

{
"manifest_version": 2,
"name": "SiteBlockerFyp",
"description": "Block Respective Sites",
"version": "1",

"permissions": [
"webRequest",
"webRequestBlocking",
"*://www.twitter.com/*",
"tabs"

],



"content_scripts": [
{
"matches": ["<all_urls>"],

"js": ["jquery.min.js", "xivelyjs.min.js", "background.js"]
}
]


}

关于javascript - Chrome 扩展程序访问 Xively,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com