Skip to content

Commit

Permalink
Allow jsPatch without content or button element in SERVICE mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Dither committed Aug 26, 2016
1 parent f07ce35 commit f97c0ee
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 72 deletions.
1 change: 0 additions & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<preference name="custom_info" value="" />
<preference name="site_fail_stats" value="" />
<preference name="disabled_sites" value="" />
<preference name="disabled_sites" value="" />
<preference name="siteinfo_wedata" value="" />
<license href="http://www.opensource.org/licenses/mit-license.php">Licensed under the Open Source Initiative OSI - The MIT License (MIT);
you may not use this file except in compliance with the License.
Expand Down
145 changes: 77 additions & 68 deletions includes/autopatchwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
};

var browser_type, checksum = new FastCRC32, debug = false, profiler = false, dump_request = false,
BROWSER_CHROME = 1,
BROWSER_SAFARI = 2,
BROWSER_OPERA = 3;
BROWSER_CHROME = 1,
BROWSER_SAFARI = 2,
BROWSER_OPERA = 3;

var options = {
BASE_REMAINING_HEIGHT: 800,
Expand Down Expand Up @@ -234,6 +234,9 @@
* @return {Node} Matched node.
* */
function get_node(doc, path) {
if (typeof doc === 'string') {
return document.querySelector(doc);
}
return doc.querySelector(path);
}

Expand All @@ -243,6 +246,9 @@
* @return {Node} Matched node.
* */
function get_node_xpath(doc, path) {
if (typeof doc === 'string') {
return document.evaluate(doc, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
//if (typeof doc.selectSingleNode === 'undefined')
return doc.evaluate(path, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
//else return doc.selectSingleNode(path);
Expand Down Expand Up @@ -382,7 +388,7 @@
return AutoPatchWork(s) || (fails.push(s), false);
});
if (ready === false) sendRequest({ failed_siteinfo: fails });
return dispatch_event('AutoPatchWork.ready');
dispatch_event('AutoPatchWork.ready');
}

/**
Expand All @@ -406,20 +412,55 @@
function AutoPatchWork(siteinfo) {
if (window.AutoPatchWorked.already) return true;

var base_url,
downloaded_pages = [],
page_titles = [],
scroll = false,
loaded_url = null,
requested_urls = {},
loaded_crcs = {};
var base_url = '',
downloaded_pages = [],
page_titles = [],
scroll = false,
loaded_url = null,
requested_urls = {},
loaded_crcs = {};

status.service = s2b(siteinfo.SERVICE);
if(!status.service) {
status.change_address = typeof siteinfo.forceAddressChange !== 'undefined' ? !s2b(siteinfo.forceAddressChange) ^ !options.CHANGE_ADDRESS : options.CHANGE_ADDRESS;
}

status.css_patch = siteinfo.cssPatch || null;
status.js_patch = siteinfo.jsPatch || null;
if (status.js_patch) {
run_script(status.js_patch);
if (status.js_patch) run_script(status.js_patch);

status.page_elem = siteinfo.pageElement || null;
status.page_elem_selector = siteinfo.pageElementSelector || null;
if (status.page_elem) try {
document.querySelector(status.page_elem);
status.page_elem_selector = status.page_elem;
status.page_elem = null;
} catch (bug) {
status.page_elem_selector = null;
}

status.button_elem = siteinfo.buttonElement || null;
status.button_elem_selector = siteinfo.buttonElementSelector || null;
if (status.button_elem) try {
document.querySelector(status.button_elem);
status.button_elem_selector = status.button_elem;
status.button_elem = null;
} catch (bug) {
status.button_elem_selector = null;
}

status.remove_elem = siteinfo.removeElement || null;
status.remove_elem_selector = siteinfo.removeElementSelector || null;
if (status.remove_elem) try {
document.querySelector(status.remove_elem);
status.remove_elem_selector = status.remove_elem;
status.remove_elem = null;
} catch (bug) {
status.remove_elem_selector = null;
}

if (!status.page_elem && !status.page_elem_selector && !status.button_elem && !status.button_elem_selector && status.service) return true;

status.next_link = siteinfo.nextLink || null;
status.next_link_selector = siteinfo.nextLinkSelector || null;
if (status.next_link) {
Expand Down Expand Up @@ -450,50 +491,15 @@
}

status.id = parseInt(siteinfo['wedata.net.id'], 10) || -1;

status.page_elem = siteinfo.pageElement || null;
status.page_elem_selector = siteinfo.pageElementSelector || null;
if (status.page_elem) try {
document.querySelector(status.page_elem);
status.page_elem_selector = status.page_elem;
status.page_elem = null;
} catch (bug) {
status.page_elem_selector = null;
}

status.button_elem = siteinfo.buttonElement || null;
status.button_elem_selector = siteinfo.buttonElementSelector || null;
if (status.button_elem) try {
document.querySelector(status.button_elem);
status.button_elem_selector = status.button_elem;
status.button_elem = null;
} catch (bug) {
status.button_elem_selector = null;
}

status.remove_elem = siteinfo.removeElement || null;
status.remove_elem_selector = siteinfo.removeElementSelector || null;
if (status.remove_elem) try {
document.querySelector(status.remove_elem);
status.remove_elem_selector = status.remove_elem;
status.remove_elem = null;
} catch (bug) {
status.remove_elem_selector = null;
}

status.separator_disabled = s2b(siteinfo.disableSeparator);
status.scripts_allowed = s2b(siteinfo.allowScripts);
status.use_iframe_req = typeof siteinfo.forceIframe !== 'undefined' ? s2b(siteinfo.forceIframe) : null;
status.service = s2b(siteinfo.SERVICE);
if(!status.service) {
status.change_address = typeof siteinfo.forceAddressChange !== 'undefined' ? !s2b(siteinfo.forceAddressChange) ^ !options.CHANGE_ADDRESS : options.CHANGE_ADDRESS;
}

if (!s2b(siteinfo.MICROFORMAT)) log('detected SITEINFO = ' + JSON.stringify(siteinfo, null, 4));

var not_button_mode = (!status.button_elem && !status.button_elem_selector),
next = not_button_mode ? get_next_link(document) : null,
page_elements = not_button_mode ? get_main_content(document) : null;
next = not_button_mode ? get_next_link(document) : null,
page_elements = not_button_mode ? get_main_content(document) : null;

if (!status.service) {
if (status.button_elem) {
Expand Down Expand Up @@ -640,7 +646,7 @@
if (typeof siteinfo.pause !== 'undefined' && !siteinfo.pause) state_on();
status.bar && status.bar.removeAttribute('style');

remove_nodes(document);
remove_nodes();

verify_scroll();

Expand Down Expand Up @@ -1124,6 +1130,7 @@
* @param {documentElement} doc Base DOM element to remove nodes from.
* */
function remove_nodes(doc) {
if (typeof doc === 'undefined') doc = document;
// filter undesired elements
if (!status.remove_elem && !status.remove_elem_selector) return;
var r, l;
Expand All @@ -1150,26 +1157,28 @@
if (!event.detail || !event.detail.htmlDoc)
return dispatch_event('AutoPatchWork.error', { message: 'no response from server' });

loaded_url = event.detail.url;
base_url = get_base_url(loaded_url);
next = get_next_link(event.detail.htmlDoc);

parse_links(next);

remove_nodes(event.detail.htmlDoc);
if (typeof event.detail.url === 'string' && event.detail.url.length) {
loaded_url = event.detail.url;
base_url = get_base_url(loaded_url);
}

if (!status.scripts_allowed) { // filter scripts
for (var i = 0, st = event.detail.htmlDoc.querySelectorAll('script'), len = st.length; i < len; i++)
if (st[i].parentNode)
st[i].parentNode.removeChild(st[i]);
if (!status.service) {
next = get_next_link(event.detail.htmlDoc);
parse_links(next);
remove_nodes(event.detail.htmlDoc);
if (!status.scripts_allowed) { // filter scripts
for (var i = 0, st = event.detail.htmlDoc.querySelectorAll('script'), len = st.length; i < len; i++)
if (st[i].parentNode)
st[i].parentNode.removeChild(st[i]);
}
}

status.page_number++;
document.apwpagenumber++;

var title_node = event.detail.htmlDoc.querySelector('title'),
nodes = get_main_content(event.detail.htmlDoc),
title = title_node ? title_node.textContent.trim() : '';
nodes = get_main_content(event.detail.htmlDoc),
title = title_node ? title_node.textContent.trim() : '';
delete event.detail.htmlDoc;

if (!nodes || !nodes.length) return dispatch_event('AutoPatchWork.error', { message: 'page content ' + (status.page_elem || status.page_elem_selector) + ' not found' });
Expand Down Expand Up @@ -1201,12 +1210,12 @@
return dispatch_event('AutoPatchWork.error', { message: 'page content is not specified' });;

var inserted_node, tmp_title,
content_last = status.content_last,
content_parent = status.content_parent,
change_location = status.change_address;
content_last = status.content_last,
content_parent = status.content_parent,
change_location = status.change_address;

var nodes = event.detail.nodes,
title = event.detail.title;
title = event.detail.title;

delete event.detail.nodes;
delete event.detail.title;
Expand Down
6 changes: 3 additions & 3 deletions scripts/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ document.addEventListener('DOMContentLoaded',function(){
};

function L10N() {
i18n.getAcceptLanguages(function(langs) {
if(langs.indexOf('ja') < 0) {
/*i18n.getAcceptLanguages(function(langs) {
if (langs.indexOf('ja') < 0) {
// We should probably load history from language JSONs via AJAX
document.querySelector('#menu-tabs > #menu-news').style.display = 'none';
}
});
});*/
var elems = document.querySelectorAll('*[class^="MSG_"]');
Array.prototype.forEach.call(elems, function(node) {
var key = node.className.match(/MSG_(\w+)/)[1];
Expand Down

0 comments on commit f97c0ee

Please sign in to comment.