@@ -40,9 +40,9 @@ import java.util.Map; | |||
import static bubble.ApiConstants.HOME_DIR; | |||
import static bubble.rule.RequestModifierRule.ICON_JS_TEMPLATE; | |||
import static bubble.rule.RequestModifierRule.ICON_JS_TEMPLATE_NAME; | |||
import static org.cobbzilla.util.daemon.ZillaRuntime.die; | |||
import static org.cobbzilla.util.daemon.ZillaRuntime.empty; | |||
import static org.cobbzilla.util.daemon.ZillaRuntime.*; | |||
import static org.cobbzilla.util.io.FileUtil.abs; | |||
import static org.cobbzilla.util.io.FileUtil.basename; | |||
import static org.cobbzilla.util.io.regex.RegexReplacementFilter.DEFAULT_PREFIX_REPLACEMENT_WITH_MATCH; | |||
import static org.cobbzilla.util.json.JsonUtil.json; | |||
import static org.cobbzilla.util.security.ShaUtil.sha256_hex; | |||
@@ -127,12 +127,12 @@ public abstract class AbstractAppRuleDriver implements AppRuleDriver { | |||
protected String loadTemplate(String defaultTemplate, String templatePath) { | |||
if (configuration.getEnvironment().containsKey("DEBUG_RULE_TEMPLATES")) { | |||
final File templateFile = new File(HOME_DIR + "/debugTemplates/" + templatePath); | |||
final File templateFile = new File(HOME_DIR + "/debugTemplates/" + basename(templatePath)); | |||
if (templateFile.exists() && templateFile.lastModified() > getJarTime()) { | |||
log.error("loadTemplate: debug file found and newer than bubble jar, using it: "+abs(templateFile)); | |||
if (log.isDebugEnabled()) log.debug("loadTemplate: debug file found and newer than bubble jar, using it: "+abs(templateFile)); | |||
return FileUtil.toStringOrDie(templateFile); | |||
} else { | |||
log.error("loadTemplate: debug file not found or older than bubble jar, using default: "+abs(templateFile)); | |||
if (log.isDebugEnabled()) log.debug("loadTemplate: debug file not found or older than bubble jar, using default: "+abs(templateFile)); | |||
} | |||
} | |||
return defaultTemplate; | |||
@@ -366,7 +366,7 @@ public class BubbleBlockRuleDriver extends TrafficAnalyticsRuleDriver implements | |||
if (bubbleBlockConfig.inPageBlocks()) { | |||
return filterInsertJs(in, filterRequest, filterCtx, BUBBLE_JS_TEMPLATE, EMPTY, BLOCK_STATS_JS, showStats); | |||
} | |||
log.warn(prefix+"inserting JS for stats into: "+request.getUrl()); | |||
log.warn(prefix+"inserting JS for stats into: "+request.getUrl()+" with Content-Type: "+filterRequest.getContentType()); | |||
return filterInsertJs(in, filterRequest, filterCtx, getBubbleJsStatsTemplate(), null, null, showStats); | |||
} | |||
@@ -10,18 +10,24 @@ if (typeof {{PAGE_PREFIX}}_icon_status === 'undefined') { | |||
method: 'POST', | |||
body: logData | |||
}; | |||
console.log('Logging to server: '+logData); | |||
fetch('/__bubble/api/filter/logs/{{BUBBLE_REQUEST_ID}}', requestOptions) | |||
.then(() => { | |||
console.log('Logged to server:', logData); | |||
console.log('Logged to server: '+logData); | |||
}) | |||
.catch((error) => { | |||
console.error('Error logging "'+logData+'" to server:', error); | |||
console.error('Error logging "'+logData+'" to server: '+error); | |||
}); | |||
} | |||
{{PAGE_PREFIX}}_addBubbleApp = function (app) { | |||
if (window.self === window.top) { | |||
{{PAGE_PREFIX}}_icon_status.push(app); | |||
if ({{PAGE_PREFIX}}_icon_status.find(a => a.app === app.app)) { | |||
{{PAGE_PREFIX}}_log('addBubbleApp: NOT adding app (already added): '+app.app); | |||
} else { | |||
{{PAGE_PREFIX}}_log('addBubbleApp: adding app: '+app.app); | |||
{{PAGE_PREFIX}}_icon_status.push(app); | |||
} | |||
} | |||
} | |||
@@ -75,23 +75,32 @@ const {{JS_PREFIX}}_app_refresh = function (displayFunc) { | |||
icon = 'icon-yellow'; | |||
} else { | |||
icon = 'icon-red'; | |||
console.log('cancelling window.interval, red status'); | |||
window.clearInterval({{JS_PREFIX}}_app_refresh_interval); | |||
if ({{JS_PREFIX}}_app_refresh_interval !== null) { | |||
console.log('cancelling window.interval, red status'); | |||
window.clearInterval({{JS_PREFIX}}_app_refresh_interval); | |||
{{JS_PREFIX}}_app_refresh_interval = null; | |||
} | |||
} | |||
{{JS_PREFIX}}_app.icon = icon; | |||
{{PAGE_PREFIX}}_setAppIconImg({{JS_PREFIX}}_app); | |||
} else if (Date.now() - {{JS_PREFIX}}_app_stats_last_change > {{JS_PREFIX}}_app_stats_timeout) { | |||
} else if ({{JS_PREFIX}}_app_refresh_interval !== null && Date.now() - {{JS_PREFIX}}_app_stats_last_change > {{JS_PREFIX}}_app_stats_timeout) { | |||
console.log('cancelling window.interval, stats unchanged for a while'); | |||
window.clearInterval({{JS_PREFIX}}_app_refresh_interval); | |||
{{JS_PREFIX}}_app_refresh_interval = null; | |||
} | |||
if (typeof displayFunc === 'function') { | |||
displayFunc(); | |||
} | |||
} | |||
}).catch((error) => { | |||
console.log('cancelling window.interval, due to error: '+error); | |||
window.clearInterval({{JS_PREFIX}}_app_refresh_interval); | |||
if ({{JS_PREFIX}}_app_refresh_interval !== null) { | |||
console.log('cancelling window.interval, due to error: ' + error); | |||
window.clearInterval({{JS_PREFIX}}_app_refresh_interval); | |||
{{JS_PREFIX}}_app_refresh_interval = null; | |||
} else { | |||
console.log('error: ' + error); | |||
} | |||
}); | |||
} | |||
@@ -12,60 +12,80 @@ const {{JS_PREFIX}}_site_host = location.protocol + '//' + window.location.hostn | |||
function {{JS_PREFIX}}_apply_blocks(blocked_users) { | |||
let articles = {{JS_PREFIX}}_getElementsByXPath('//div[@role="article" and @aria-posinset]'); | |||
let sitePrefix = {{JS_PREFIX}}_site_host; | |||
const log = {{PAGE_PREFIX}}_log; | |||
for (let i=0; i<articles.length; i++) { | |||
let article = articles[i]; | |||
let authorLink = article.getElementsByTagName('a')[0]; | |||
if (typeof authorLink.href === 'undefined') { | |||
continue; | |||
} | |||
let authorHref = authorLink.href; | |||
if (!authorHref.href.startsWith(sitePrefix)) continue; | |||
if (!authorHref.startsWith(sitePrefix)) continue; | |||
let authorName = authorHref.substring(sitePrefix.length); | |||
let qPos = authorName.indexOf('?'); | |||
if (qPos !== -1) { | |||
authorName = authorName.substring(0, qPos); | |||
authorLink.href = sitePrefix + authorName; // todo: clean up all links, not just this one | |||
authorLink.href = sitePrefix + authorName; | |||
} | |||
if (blocked_users !== null && blocked_users.includes(authorName)) { | |||
console.log('removing post by author: ' + authorName); | |||
try { | |||
article.className = article.className + ' _bubble_blocked'; | |||
node.style = 'display: none'; | |||
} catch (e) { | |||
console.log('error removing post by author: ' + authorName+': '+e); | |||
if (article.className.indexOf('{{JS_PREFIX}}_bub_blocked') === -1) { | |||
log('removing post by author: ' + authorName); | |||
article.className = article.className += ' {{JS_PREFIX}}_bub_blocked'; | |||
try { | |||
const feedItem = article.parentElement.parentElement.parentElement.parentElement.parentElement; | |||
log('nuking: '+feedItem.outerHTML.substring(0, 40)) | |||
feedItem.parentElement.removeChild(feedItem); | |||
} catch (e) { | |||
log('error removing post by author: ' + authorName + ': ' + e); | |||
} | |||
} else { | |||
log('found post marked removed but still present (??) by author: ' + authorName); | |||
} | |||
} else { | |||
// have we visited this article before? | |||
if (article.className.indexOf('{{JS_PREFIX}}_bubble_block') === -1) { | |||
console.log('VISITING article node for author: '+authorName); | |||
const spans = authorLink.parentNode.nextSibling.getElementsByTagName('span'); | |||
let sepSpan = null; | |||
for (let i=0; i<spans.length; i++) { | |||
let s = spans[i]; | |||
if (s.innerHTML === ' · ') { | |||
sepSpan = s; | |||
break; | |||
if (article.innerHTML.indexOf('_bubble') === -1) { | |||
log('VISITING article node for author: '+authorName); | |||
try { | |||
article.className = article.className += ' {{JS_PREFIX}}_bubble_block'; | |||
if (typeof authorLink.parentNode.parentNode.nextSibling === 'undefined' || authorLink.parentNode.parentNode.nextSibling === null) { | |||
console.log('authorLink.parentNode.parentNode.nextSibling was undefined or null, skipping; outerHTML='+authorLink.parentNode.parentNode.outerHTML); | |||
continue; | |||
} | |||
} | |||
if (sepSpan === null) { | |||
console.log('no insertion point found'); | |||
continue; | |||
} | |||
const blockControl = document.createElement('div'); | |||
blockControl.style.textAlign = 'center'; | |||
article.className = article.className += ' {{JS_PREFIX}}_bubble_block'; | |||
const spans = authorLink.parentNode.parentNode.nextSibling.getElementsByTagName('span'); | |||
const sepSpans = Array.from(spans).filter(s => s.innerHTML === ' · '); | |||
if (sepSpans.length === 0) { | |||
log('no insertion point found for author: ' + authorName); | |||
continue; | |||
} | |||
sepSpans.map(sepSpan => { | |||
const imgHolder = document.createElement('img'); | |||
imgHolder.src = '/__bubble/api/filter/assets/{{BUBBLE_REQUEST_ID}}/UserBlocker/icon?raw=true'; | |||
imgHolder.width = 16; | |||
const line = document.createElement('hr'); | |||
const imgHolder = document.createElement('img'); | |||
imgHolder.src = '/__bubble/api/filter/assets/{{BUBBLE_REQUEST_ID}}/UserBlocker/icon?raw=true'; | |||
imgHolder.width = 16; | |||
const blockLink = document.createElement('a'); | |||
blockLink.addEventListener("click", function (event) { | |||
{{JS_PREFIX}}_block_user(authorName); | |||
try { | |||
const feedItem = article.parentElement.parentElement.parentElement.parentElement.parentElement; | |||
log('click nuking: '+feedItem.outerHTML.substring(0, 40)) | |||
feedItem.parentElement.removeChild(feedItem); | |||
} catch (e) { | |||
log('click error removing post by author: ' + authorName + ': ' + e); | |||
} | |||
return false; | |||
}); | |||
// blockLink.appendChild(line); | |||
blockLink.appendChild(imgHolder); | |||
const blockLink = document.createElement('a'); | |||
blockLink.addEventListener("click", function (event) { {{JS_PREFIX}}_block_user(authorName); return false; }); | |||
blockLink.appendChild(line); | |||
blockLink.appendChild(imgHolder); | |||
blockControl.appendChild(blockLink); | |||
// log('inserting block icon for ' + authorName + '...'); | |||
sepSpan.parentNode.appendChild(blockLink); | |||
sepSpan.parentNode.appendChild(document.createTextNode(' · ')); | |||
}); | |||
sepSpan.appendChild(blockControl); | |||
sepSpan.appendChild(document.createTextNode(' · ')); | |||
} catch (e) { | |||
log('badness: '+e); | |||
} | |||
} | |||
} | |||
} | |||