【问题标题】:Use cron to fetch a view which contains jQuery content?使用 cron 获取包含 jQuery 内容的视图?
【发布时间】:2013-04-14 21:16:56
【问题描述】:

有没有办法可以设置一个 cron 在我的 Drupal 站点上运行视图?我有一个 jQuery 脚本,我想每小时运行一次。

我试过了;

php http://mysite/myview

但我想我有点离题了。我明白了;

Could not open input file: 

这是我在我的 rss 提要中使用的 js;

// $Id
(function ($) {
Drupal.behaviors.tweetcast = {

attach: function (context, settings) {

$('div.tagspeak:not(.tags-processed)', context).addClass('tags-processed').each(function () {

var mynid, sinceid, updateinfo, sinceurl, author, tweethtml;
var currentsinceid = $('.currentsinceid').val();

var firstring = $(this).html();
var twostring = firstring.replace(/\s/g,'').replace(/^%20OR%20/gim,'').replace(/%20OR$/gim,'').replace(/%20OR%20%$/gim,'').replace(/%20OR%20$/gim,'').replace(/%$/gim,'').replace(/%20OR$/gim,'').toLowerCase();  
var threestring = twostring.replace(/%20or%20/gim,'%20OR%20');
$(this).text(threestring);
var fourstring = threestring.replace(/%20OR%20/gim,' ');
var fivestring = fourstring.replace(/%23/gim,'#');

$(this).closest('.views-row').append('<div class="views-field views-field-replies"></div>');    
tweethtml = $(this).closest('.views-row').find('div.views-field-replies').eq(0);

var twitter_api_url = 'http://search.twitter.com/search.json';
$.ajaxSetup({ cache: true });

$.getJSON( twitter_api_url + '?callback=?&rpp=1&q=' + threestring + '&since_id=' + currentsinceid, function(data) {
$.each(data.results, function(i, tweet) {console.log(tweet);

    if(tweet.text !== undefined) {

    var tweet_html = '<div class="tweetuser">' + tweet.from_user + '</div>';
    tweet_html  += ' <div class="sinceid">' + tweet.id + '</div>';
    tweethtml.append(tweet_html);

    }
});
});
    $.fn.delay = function(time, callback){
    jQuery.fx.step.delay = function(){};
    return this.animate({delay:1}, time, callback);
    }

    $(this).delay(2000, function(){
    title = $(this).closest('.views-row').find('div.views-field-title').eq(0).find('span.field-content').text();
    link = $(this).closest('.views-row').find('div.views-field-path').eq(0).find('span.field-content').text();
    author = $(this).closest('.views-row').find('div.tweetuser').eq(0).text();
    mynid = $(this).closest('.views-row').find('div.mynid').text();
    sinceid = $(this).closest('.views-row').find('div.sinceid').eq(0).text();
    updateinfo = {sinceid: sinceid, mynid: mynid, author: author, title: title, link: link, tags: fivestring};
    sinceurl = Drupal.settings.basePath + 'sinceid';

    $.ajax({
    type: "POST",
    url: sinceurl,
    data: updateinfo,
    success: function(){}
    });
    });
});
}}
})
(jQuery);

这里是我的 php 模块;

<?php
// $id:
function sinceid_menu() {
  $items = array();
  $items['sinceid'] = array(
  'title' => 'sinceid',
  'page callback' => 'sinceid_count',
  'description' => 'sinceid',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK,
  );
return $items;
}
function sinceid_count() 
{
$sinceid=$_POST['sinceid'];
$mynid=$_POST['mynid'];
$author=$_POST['author'];
$title=$_POST['title'];
$link=$_POST['link'];
$tags=$_POST['tags'];

db_update('node')
->expression('sinceid', $sinceid)
->condition('nid', $mynid)
->execute();

$sql = db_query("SELECT author FROM {authordupe} WHERE author = $author");
$result = db_query($sql);
$how_many_rows = $result->rowCount();
if($how_many_rows == 0) {

db_insert('authordupe')
->fields(array(
'author' => $author,
'title' => $title,
'link' => $link,
'tags' => $tags,
))
->execute();



}   
}

【问题讨论】:

    标签: php jquery drupal-7 cron


    【解决方案1】:

    在 Drupal 中运行视图的基本代码是

    $view = views_get_view('<your_view_machine_name>');
    $view->set_display('<optional_display_name>');
    $view->execute();
    

    也就是说,你说有一个在视图中运行的 jQuery 脚本。严重地?你的脚本在做什么?我会说当您希望脚本在客户端运行时,最好使用 jQuery。难道你不能编写php代码来完成你需要的任何事情并把它放在一个cron hook中吗?

    【讨论】:

    • Ummm... 很尴尬但是... 我不是 php 程序员。我知道一点jquery。我知道这很丑……但我的能力有限……我将编辑我的原始帖子并添加代码。我有创建 rss 提要的视图,我正在使用 jquery 中的正则表达式对其进行修改并发布到 twitter。
    • 这里的问题是您正在尝试执行一个 jQuery 脚本,该脚本通常在浏览器中运行。我仍然认为您最好在模块中使用 cron 挂钩来加载适当的节点并构建 rss 以发布到 twitter。或者,如果您仍然认为自己更喜欢 jQuery 解决方案,您可以尝试PhantomJS(我自己从未使用过,但看起来很有希望......)
    • 感谢您指出正确的方法!我会采纳你的建议来编写一个模块,我也会考虑使用看起来很有希望的 PhantomJS。
    猜你喜欢
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多